Implement better constant string management

This commit is contained in:
David Schmenk 2017-06-07 10:07:14 -07:00
parent 3f880ecb67
commit 7ee5a3d524
4 changed files with 46 additions and 43 deletions

View File

@ -482,7 +482,7 @@ void emit_idconst(char *name, int value)
int emit_data(int vartype, int consttype, long constval, int constsize)
{
int datasize, i;
char *str;
unsigned char *str;
if (consttype == 0)
{
datasize = constsize;
@ -490,9 +490,10 @@ int emit_data(int vartype, int consttype, long constval, int constsize)
}
else if (consttype & STRING_TYPE)
{
datasize = constsize;
str = (char *)constval;
printf("\t%s\t$%02X\n", DB, --constsize);
str = (unsigned char *)constval;
constsize = *str++;
datasize = constsize + 1;
printf("\t%s\t$%02X\n", DB, constsize);
while (constsize-- > 0)
{
printf("\t%s\t$%02X", DB, *str++);
@ -555,10 +556,10 @@ void emit_const(int cval)
else
printf("\t%s\t$2C,$%02X,$%02X\t\t; CW\t%d\n", DB, cval&0xFF,(cval>>8)&0xFF, cval);
}
void emit_conststr(long conststr, int strsize)
void emit_conststr(long conststr)
{
printf("\t%s\t$2E\t\t\t; CS\n", DB);
emit_data(0, STRING_TYPE, conststr, strsize);
emit_data(0, STRING_TYPE, conststr, 0);
}
void emit_lb(void)
{
@ -1264,7 +1265,7 @@ int emit_seq(t_opseq *seq)
emit_const(op->val);
break;
case STR_CODE:
emit_conststr(op->val, op->offsz);
emit_conststr(op->val);
break;
case LB_CODE:
emit_lb();

View File

@ -74,7 +74,7 @@ typedef struct _opseq {
#define gen_uop(seq,op) gen_seq(seq,UNARY_CODE(op),0,0,0,0)
#define gen_op(seq,op) gen_seq(seq,BINARY_CODE(op),0,0,0,0)
#define gen_const(seq,val) gen_seq(seq,CONST_CODE,val,0,0,0)
#define gen_str(seq,str,len) gen_seq(seq,STR_CODE,str,0,len,0)
#define gen_str(seq,str) gen_seq(seq,STR_CODE,str,0,0,0)
#define gen_lcladr(seq,idx) gen_seq(seq,LADDR_CODE,0,0,idx,0)
#define gen_gbladr(seq,tag,typ) gen_seq(seq,GADDR_CODE,0,tag,0,typ)
#define gen_idxb(seq) gen_seq(seq,ADD_CODE,0,0,0,0)
@ -103,7 +103,7 @@ void emit_idconst(char *name, int value);
int emit_data(int vartype, int consttype, long constval, int constsize);
void emit_codetag(int tag);
void emit_const(int cval);
void emit_conststr(long conststr, int strsize);
void emit_conststr(long conststr);
void emit_lb(void);
void emit_lw(void);
void emit_llb(int index);

View File

@ -7,7 +7,6 @@
* ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
@ -15,7 +14,7 @@
#include <ctype.h>
#include "plasm.h"
char *statement, *tokenstr, *scanpos = (char*) "";
char *statement, *tokenstr, *scanpos = "", *strpos = "";
t_token scantoken, prevtoken;
int tokenlen;
long constval;
@ -234,68 +233,68 @@ t_token scan(void)
scanpos += 4;
}
}
else if ((scanpos[0] & 0x7F) == '\"') // Hack for string quote char in case we have to rewind later
else if (scanpos[0] == '\"') // Hack for string quote char in case we have to rewind later
{
char *scanshift, quotechar;
int scanoffset;
/*
* String constant.
*/
quotechar = scanpos[0];
*scanpos |= 0x80; // Set high bit in case of rewind
scantoken = STRING_TOKEN;
constval = (long)++scanpos;
while (*scanpos && *scanpos != quotechar)
scantoken = STRING_TOKEN;
constval = (long)strpos++;
scanpos++;
while (*scanpos && *scanpos != '\"')
{
if (*scanpos == '\\')
{
scanoffset = 1;
scanoffset = 2;
switch (scanpos[1])
{
case 'n':
*scanpos = 0x0D;
*strpos++ = 0x0D;
break;
case 'r':
*scanpos = 0x0A;
*strpos++ = 0x0A;
break;
case 't':
*scanpos = '\t';
*strpos++ = '\t';
break;
case '\'':
*scanpos = '\'';
*strpos++ = '\'';
break;
case '\"':
*scanpos = '\"';
*strpos++ = '\"';
break;
case '\\':
*scanpos = '\\';
*strpos++ = '\\';
break;
case '0':
*scanpos = '\0';
*strpos++ = '\0';
break;
case '$':
if (hexdigit(scanpos[2]) < 0 || hexdigit(scanpos[3]) < 0) {
parse_error("Bad string constant");
return (-1);
}
*scanpos = hexdigit(scanpos[2]) * 16 + hexdigit(scanpos[3]);
scanoffset = 3;
*strpos++ = hexdigit(scanpos[2]) * 16 + hexdigit(scanpos[3]);
scanoffset = 4;
break;
default:
parse_error("Bad string constant");
return (-1);
}
for (scanshift = scanpos + 1; *scanshift; scanshift++)
scanshift[0] = scanshift[scanoffset];
scanpos += scanoffset;
}
scanpos++;
else
*strpos++ = *scanpos++;
}
if (!*scanpos)
{
parse_error("Unterminated string");
return (-1);
}
*scanpos++ |= 0x80; // Set high bit in case of rewind
*((unsigned char *)constval) = (long)strpos - constval - 1;
*strpos++ = '\0';
scanpos++;
}
else
{
@ -414,30 +413,34 @@ void scan_rewind(char *backptr)
}
int scan_lookahead(void)
{
char *backpos = scanpos;
char *backstr = tokenstr;
char *backscan = scanpos;
char *backtkn = tokenstr;
char *backstr = strpos;
int prevtoken = scantoken;
int prevlen = tokenlen;
int prevlen = tokenlen;
int look = scan();
scanpos = backpos;
tokenstr = backstr;
scanpos = backscan;
tokenstr = backtkn;
strpos = backstr;
scantoken = prevtoken;
tokenlen = prevlen;
return (look);
}
char inputline[512];
char conststr[1024];
int next_line(void)
{
int len;
t_token token;
char* new_filename;
strpos = conststr;
if (inputfile == NULL)
{
/*
* First-time init
*/
inputfile = stdin;
filename = (char*) "<stdin>";
filename = "<stdin>";
}
if (*scanpos == ';')
{
@ -501,9 +504,8 @@ int next_line(void)
outer_inputfile = inputfile;
outer_filename = filename;
outer_lineno = lineno;
new_filename = (char*) malloc(tokenlen-1);
strncpy(new_filename, (char*)constval, tokenlen-2);
new_filename[tokenlen-2] = 0;
new_filename = (char *) malloc(*((unsigned char *)constval) + 1);
strncpy(new_filename, (char *)(constval + 1), *((unsigned char *)constval) + 1);
inputfile = fopen(new_filename, "r");
if (inputfile == NULL)
{

View File

@ -229,7 +229,7 @@ int parse_constval(void)
case CLOSE_PAREN_TOKEN:
break;
case STRING_TOKEN:
size = tokenlen - 1;
size = 1;
value = constval;
type = STRING_TYPE;
if (mod)
@ -421,7 +421,7 @@ t_opseq *parse_value(t_opseq *codeseq, int rvalue, int *stackdepth)
/*
* This is a special case. Just emit the string and return
*/
codeseq = gen_str(codeseq, constval, tokenlen - 1);
codeseq = gen_str(codeseq, constval);
scan();
return (codeseq);
}