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

View File

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

View File

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