From 4f099e7eb1b196b8360e9d914bc6895fdc0c7dca Mon Sep 17 00:00:00 2001 From: Daniel_Cortez Date: Wed, 31 Jan 2018 04:09:05 +0700 Subject: [PATCH 01/15] emit: Disallow the use of the '-' sign with function names for arguments of type 'any' --- source/compiler/sc1.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/source/compiler/sc1.c b/source/compiler/sc1.c index b1ad703..f2a2465 100644 --- a/source/compiler/sc1.c +++ b/source/compiler/sc1.c @@ -5984,6 +5984,8 @@ fetchtok: goto invalid_token; } /* if */ if (sym->ident==iFUNCTN || sym->ident==iREFFUNC) { + if (neg!=FALSE) + goto invalid_token_neg; if ((sym->usage & uNATIVE)!=0 && (sym->usage & uREAD)==0 && sym->addr>=0) sym->addr=ntv_funcid++; markusage(sym,uREAD); @@ -6012,9 +6014,10 @@ fetchtok: neg=TRUE; goto fetchtok; } else { - char ival[sNAMEMAX+2]="-"; - strcpy(ival+1,str); - error(1,sc_tokens[tSYMBOL-tFIRST],ival); + char ival[sNAMEMAX+2]; + invalid_token_neg: + sprintf(ival,"-%s",str); + error(1,sc_tokens[teNUMERIC-tFIRST],ival); break; } /* if */ default: From ca01cbdf7d3d32a679b110be674361f95b9f6f91 Mon Sep 17 00:00:00 2001 From: Daniel_Cortez Date: Wed, 31 Jan 2018 06:10:23 +0700 Subject: [PATCH 02/15] emit: Redo instruction output --- source/compiler/sc.h | 12 ++++- source/compiler/sc1.c | 109 ++++++++++++++++++++---------------------- source/compiler/sc4.c | 17 +++++-- 3 files changed, 76 insertions(+), 62 deletions(-) diff --git a/source/compiler/sc.h b/source/compiler/sc.h index 48d43fd..78bdd78 100644 --- a/source/compiler/sc.h +++ b/source/compiler/sc.h @@ -475,6 +475,16 @@ typedef enum s_optmark { #define MAX_INSTR_LEN 30 +#define eotNUMBER 0 +#define eotFUNCTION 1 +typedef struct s_emit_outval { + int type; + union { + ucell ucell; + const char *string; + } value; +} emit_outval; + /* interface functions */ #if defined __cplusplus extern "C" { @@ -699,7 +709,7 @@ SC_FUNC void dec(value *lval); SC_FUNC void jmp_ne0(int number); SC_FUNC void jmp_eq0(int number); SC_FUNC void outval(cell val,int newline); -SC_FUNC void outinstr(const char *name,ucell args[],int numargs); +SC_FUNC void outinstr(const char *name, emit_outval params[],int numparams); /* function prototypes in SC5.C */ SC_FUNC int error(int number,...); diff --git a/source/compiler/sc1.c b/source/compiler/sc1.c index f2a2465..de2bba3 100644 --- a/source/compiler/sc1.c +++ b/source/compiler/sc1.c @@ -5953,7 +5953,7 @@ static void SC_FASTCALL emit_invalid_token(int expected_token,int found_token) } /* if */ } -static void SC_FASTCALL emit_param_any(ucell *p) +static void SC_FASTCALL emit_param_any(emit_outval *p) { char *str; cell val,cidx; @@ -5962,14 +5962,15 @@ static void SC_FASTCALL emit_param_any(ucell *p) int tok,neg,ident,index; neg=FALSE; + p->type=eotNUMBER; fetchtok: tok=lex(&val,&str); switch (tok) { case tNUMBER: - *p=(neg!=FALSE) ? -val : val; + p->value.ucell=(ucell)((neg!=FALSE) ? -val : val); break; case tRATIONAL: - *p=(neg!=FALSE) ? (val|((cell)1 << (PAWN_CELL_SIZE-1))) : val; + p->value.ucell=(ucell)((neg!=FALSE) ? (val|((cell)1 << (PAWN_CELL_SIZE-1))) : val); break; case tSYMBOL: sym=findloc(str); @@ -5989,10 +5990,12 @@ fetchtok: if ((sym->usage & uNATIVE)!=0 && (sym->usage & uREAD)==0 && sym->addr>=0) sym->addr=ntv_funcid++; markusage(sym,uREAD); + p->type=eotFUNCTION; + p->value.string=str; } else { markusage(sym,uREAD | uWRITTEN); + p->value.ucell=(ucell)((neg!=FALSE) ? -sym->addr : sym->addr); } /* if */ - *p=(neg!=FALSE) ? -sym->addr : sym->addr; break; case '(': if ((emit_flags & efEXPR)==0) @@ -6007,7 +6010,7 @@ fetchtok: if ((emit_flags & efEXPR)==0) stgset(FALSE); needtoken(')'); - *p=(neg!=FALSE) ? -val : val; + p->value.ucell=(ucell)((neg!=FALSE) ? -val : val); break; case '-': if (neg==FALSE) { @@ -6026,17 +6029,18 @@ fetchtok: } /* switch */ } -static void SC_FASTCALL emit_param_data(ucell *p) +static void SC_FASTCALL emit_param_data(emit_outval *p) { cell val; char *str; symbol *sym; int tok; + p->type=eotNUMBER; tok=lex(&val,&str); switch (tok) { case tNUMBER: - *p=val; + p->value.ucell=(ucell)val; break; case tSYMBOL: sym=findloc(str); @@ -6061,7 +6065,7 @@ static void SC_FASTCALL emit_param_data(ucell *p) } /* if */ } /* if */ markusage(sym,uREAD | uWRITTEN); - *p=sym->addr; + p->value.ucell=(ucell)sym->addr; break; default: invalid_token: @@ -6069,17 +6073,18 @@ static void SC_FASTCALL emit_param_data(ucell *p) } /* switch */ } -static void SC_FASTCALL emit_param_local(ucell *p) +static void SC_FASTCALL emit_param_local(emit_outval *p) { cell val; char *str; symbol *sym; int tok; + p->type=eotNUMBER; tok=lex(&val,&str); switch (tok) { case tNUMBER: - *p=val; + p->value.ucell=(ucell)val; break; case tSYMBOL: sym=findloc(str); @@ -6100,7 +6105,7 @@ static void SC_FASTCALL emit_param_local(ucell *p) } /* if */ } /* if */ markusage(sym,uREAD | uWRITTEN); - *p=sym->addr; + p->value.ucell=(ucell)sym->addr; break; default: invalid_token: @@ -6108,7 +6113,7 @@ static void SC_FASTCALL emit_param_local(ucell *p) } /* switch */ } -static void SC_FASTCALL emit_param_index(ucell *p,const cell *valid_values,int numvalues) +static void SC_FASTCALL emit_param_index(emit_outval *p,const cell *valid_values,int numvalues) { cell val; char *str; @@ -6168,14 +6173,15 @@ fetchtok: return; } /* switch */ - *p=val; + p->type=eotNUMBER; + p->value.ucell=(ucell)val; for (i=0; iusage|=uREAD; - *p=*(ucell *)&sym->addr; + p->type=eotNUMBER; + p->value.ucell=(ucell)sym->addr; } -static void SC_FASTCALL emit_param_function(ucell *p,int isnative) +static void SC_FASTCALL emit_param_function(emit_outval *p,int isnative) { cell val; char *str; @@ -6222,9 +6229,11 @@ static void SC_FASTCALL emit_param_function(ucell *p,int isnative) if (isnative!=FALSE) { if ((sym->usage & uREAD)==0 && sym->addr>=0) sym->addr=ntv_funcid++; - *p=sym->addr; + p->type=eotNUMBER; + p->value.ucell=(ucell)sym->addr; } else { - *(char **)p=str; + p->type=eotFUNCTION; + p->value.string=str; } /* if */ markusage(sym,uREAD); } @@ -6241,7 +6250,7 @@ static void SC_FASTCALL emit_parm0(char *name) static void SC_FASTCALL emit_parm1_any(char *name) { - ucell p[1]; + emit_outval p[1]; emit_param_any(&p[0]); outinstr(name,p,(sizeof p / sizeof p[0])); @@ -6249,7 +6258,7 @@ static void SC_FASTCALL emit_parm1_any(char *name) static void SC_FASTCALL emit_parm1_data(char *name) { - ucell p[1]; + emit_outval p[1]; emit_param_data(&p[0]); outinstr(name,p,(sizeof p / sizeof p[0])); @@ -6257,7 +6266,7 @@ static void SC_FASTCALL emit_parm1_data(char *name) static void SC_FASTCALL emit_parm1_local(char *name) { - ucell p[1]; + emit_outval p[1]; emit_param_local(&p[0]); outinstr(name,p,(sizeof p / sizeof p[0])); @@ -6265,7 +6274,7 @@ static void SC_FASTCALL emit_parm1_local(char *name) static void SC_FASTCALL emit_parm1_label(char *name) { - ucell p[1]; + emit_outval p[1]; emit_param_label(&p[0]); outinstr(name,p,(sizeof p / sizeof p[0])); @@ -6273,36 +6282,29 @@ static void SC_FASTCALL emit_parm1_label(char *name) static void SC_FASTCALL emit_do_casetbl(char *name) { - ucell p[2]; + emit_outval p[2]; (void)name; emit_param_any(&p[0]); emit_param_label(&p[1]); stgwrite("\tcasetbl\n"); - stgwrite("\tcase "); - outval(p[0],FALSE); - stgwrite(" "); - outval(p[1],TRUE); - code_idx+=opcodes(1)+opargs(2); + outinstr("case",p,(sizeof p / sizeof p[0])); } static void SC_FASTCALL emit_do_case(char *name) { - ucell p[2]; + emit_outval p[2]; emit_param_any(&p[0]); emit_param_label(&p[1]); - stgwrite("\tcase "); - outval(p[0],FALSE); - stgwrite(" "); - outval(p[1],TRUE); - code_idx+=opargs(2); + outinstr("case",p,(sizeof p / sizeof p[0])); + code_idx-=opcodes(1); } static void SC_FASTCALL emit_do_lodb_strb(char *name) { static const cell valid_values[] = { 1,2,4 }; - ucell p[1]; + emit_outval p[1]; emit_param_index(&p[0],valid_values,(sizeof valid_values / sizeof valid_values[0])); outinstr(name,p,(sizeof p / sizeof p[0])); @@ -6311,7 +6313,7 @@ static void SC_FASTCALL emit_do_lodb_strb(char *name) static void SC_FASTCALL emit_do_lctrl(char *name) { static const cell valid_values[] = { 0,1,2,3,4,5,6,7,8,9 }; - ucell p[1]; + emit_outval p[1]; emit_param_index(&p[0],valid_values,(sizeof valid_values / sizeof valid_values[0])); outinstr(name,p,(sizeof p / sizeof p[0])); @@ -6320,7 +6322,7 @@ static void SC_FASTCALL emit_do_lctrl(char *name) static void SC_FASTCALL emit_do_sctrl(char *name) { static const cell valid_values[] = { 2,4,5,6,8,9 }; - ucell p[1]; + emit_outval p[1]; emit_param_index(&p[0],valid_values,(sizeof valid_values / sizeof valid_values[0])); outinstr(name,p,(sizeof p / sizeof p[0])); @@ -6328,22 +6330,15 @@ static void SC_FASTCALL emit_do_sctrl(char *name) static void SC_FASTCALL emit_do_call(char *name) { - char *funcname=NULL; + emit_outval p[1]; - emit_param_function((ucell *)&funcname,FALSE); - stgwrite("\t"); - stgwrite(name); - if (funcname!=NULL) { - stgwrite(" ."); - stgwrite(funcname); - } /* if */ - stgwrite("\n"); - code_idx+=opcodes(1)+opargs(1); + emit_param_function(&p[0],FALSE); + outinstr(name,p,(sizeof p / sizeof p[0])); } static void SC_FASTCALL emit_do_sysreq_c(char *name) { - ucell p[1]; + emit_outval p[1]; emit_param_function(&p[0],TRUE); @@ -6362,7 +6357,7 @@ static void SC_FASTCALL emit_do_sysreq_c(char *name) static void SC_FASTCALL emit_do_sysreq_n(char *name) { - ucell p[2]; + emit_outval p[2]; emit_param_function(&p[0],TRUE); emit_param_any(&p[1]); @@ -6378,14 +6373,14 @@ static void SC_FASTCALL emit_do_sysreq_n(char *name) } else { outinstr("push.c",&p[1],1); outinstr("sysreq.c",&p[0],1); - p[1]+=sizeof(cell); + p[1].value.ucell+=sizeof(cell); outinstr("stack",&p[1],1); } /* if */ } static void SC_FASTCALL emit_do_const(char *name) { - ucell p[2]; + emit_outval p[2]; emit_param_data(&p[0]); emit_param_any(&p[1]); @@ -6409,7 +6404,7 @@ static void SC_FASTCALL emit_do_const(char *name) static void SC_FASTCALL emit_do_const_s(char *name) { - ucell p[2]; + emit_outval p[2]; emit_param_local(&p[0]); emit_param_any(&p[1]); @@ -6433,7 +6428,7 @@ static void SC_FASTCALL emit_do_const_s(char *name) static void SC_FASTCALL emit_do_load_both(char *name) { - ucell p[2]; + emit_outval p[2]; emit_param_data(&p[0]); emit_param_data(&p[1]); @@ -6453,7 +6448,7 @@ static void SC_FASTCALL emit_do_load_both(char *name) static void SC_FASTCALL emit_do_load_s_both(char *name) { - ucell p[2]; + emit_outval p[2]; emit_param_local(&p[0]); emit_param_local(&p[1]); @@ -6473,7 +6468,7 @@ static void SC_FASTCALL emit_do_load_s_both(char *name) static void SC_FASTCALL emit_do_pushn_c(char *name) { - ucell p[5]; + emit_outval p[5]; int i,numargs; assert(name[0]=='p' && name[1]=='u' && name[2]=='s' @@ -6495,7 +6490,7 @@ static void SC_FASTCALL emit_do_pushn_c(char *name) static void SC_FASTCALL emit_do_pushn(char *name) { - ucell p[5]; + emit_outval p[5]; int i,numargs; assert(name[0]=='p' && name[1]=='u' && name[2]=='s' @@ -6517,7 +6512,7 @@ static void SC_FASTCALL emit_do_pushn(char *name) static void SC_FASTCALL emit_do_pushn_s_adr(char *name) { - ucell p[5]; + emit_outval p[5]; int i,numargs; assert(name[0]=='p' && name[1]=='u' && name[2]=='s' diff --git a/source/compiler/sc4.c b/source/compiler/sc4.c index 2d9e2b8..b335ddb 100644 --- a/source/compiler/sc4.c +++ b/source/compiler/sc4.c @@ -1379,19 +1379,28 @@ SC_FUNC void outval(cell val,int newline) } /* write an instruction with arguments */ -SC_FUNC void outinstr(const char *name,ucell args[],int numargs) +SC_FUNC void outinstr(const char *name, emit_outval params[],int numparams) { int i; stgwrite("\t"); stgwrite(name); - for (i=0; i Date: Wed, 31 Jan 2018 07:07:24 +0700 Subject: [PATCH 03/15] emit: Add new argument type: 'non-negative integer' --- source/compiler/sc.h | 5 ++-- source/compiler/sc1.c | 67 +++++++++++++++++++++++++++++++++++++++---- source/compiler/sc2.c | 2 +- 3 files changed, 65 insertions(+), 9 deletions(-) diff --git a/source/compiler/sc.h b/source/compiler/sc.h index 78bdd78..46ff20a 100644 --- a/source/compiler/sc.h +++ b/source/compiler/sc.h @@ -403,9 +403,10 @@ typedef struct s_valuepair { #define teLOCAL 341 /* local variable (name or offset) */ #define teFUNCTN 342 /* Pawn function */ #define teNATIVE 343 /* native function */ +#define teNONNEG 344 /* nonnegative integer */ /* for assigment to "lastst" only (see SC1.C) */ -#define tEXPR 344 -#define tENDLESS 345 /* endless loop */ +#define tEXPR 345 +#define tENDLESS 346 /* endless loop */ /* (reversed) evaluation of staging buffer */ #define sSTARTREORDER 0x01 diff --git a/source/compiler/sc1.c b/source/compiler/sc1.c index de2bba3..8132c18 100644 --- a/source/compiler/sc1.c +++ b/source/compiler/sc1.c @@ -6181,6 +6181,53 @@ fetchtok: error(50); /* invalid range */ } +static void SC_FASTCALL emit_param_nonneg(emit_outval *p) +{ + cell val; + char *str; + symbol *sym; + int tok,global; + + tok=lex(&val,&str); + switch (tok) { + case tNUMBER: + break; + case tSYMBOL: + global=FALSE; + sym=findloc(str); + if (sym==NULL) { + global=TRUE; + sym=findglb(str,sSTATEVAR); + } /* if */ + if (sym==NULL) { + error(17,str); /* undefined symbol */ + return; + } /* if */ + switch (sym->ident) { + case iCONSTEXPR: + break; + case iLABEL: + tok=tLABEL; + goto invalid_token; + case iFUNCTN: + case iREFFUNC: + tok=((sym->usage & uNATIVE)!=0) ? teNATIVE : teFUNCTN; + goto invalid_token; + default: + tok=(global==FALSE && sym->vclass!=sSTATIC) ? teLOCAL : teDATA; + goto invalid_token; + } /* switch */ + markusage(sym,uREAD); + default: + invalid_token: + emit_invalid_token(teNONNEG,tok); + return; + } /* switch */ + + p->type=eotNUMBER; + p->value.ucell=(ucell)val; +} + static void SC_FASTCALL emit_param_label(emit_outval *p) { cell val; @@ -6280,12 +6327,20 @@ static void SC_FASTCALL emit_parm1_label(char *name) outinstr(name,p,(sizeof p / sizeof p[0])); } +static void SC_FASTCALL emit_parm1_nonneg(char *name) +{ + emit_outval p[1]; + + emit_param_nonneg(&p[0]); + outinstr(name,p,(sizeof p / sizeof p[0])); +} + static void SC_FASTCALL emit_do_casetbl(char *name) { emit_outval p[2]; (void)name; - emit_param_any(&p[0]); + emit_param_nonneg(&p[0]); emit_param_label(&p[1]); stgwrite("\tcasetbl\n"); outinstr("case",p,(sizeof p / sizeof p[0])); @@ -6542,13 +6597,13 @@ static EMIT_OPCODE emit_opcodelist[] = { { 30, "align.alt", emit_parm1_any }, { 29, "align.pri", emit_parm1_any }, { 81, "and", emit_parm0 }, - {121, "bounds", emit_parm1_any }, + {121, "bounds", emit_parm1_nonneg }, {137, "break", emit_parm0 }, { 49, "call", emit_do_call }, { 50, "call.pri", emit_parm0 }, { 0, "case", emit_do_case }, {130, "casetbl", emit_do_casetbl }, - {118, "cmps", emit_parm1_any }, + {118, "cmps", emit_parm1_nonneg }, {156, "const", emit_do_const }, { 12, "const.alt", emit_parm1_any }, { 11, "const.pri", emit_parm1_any }, @@ -6561,10 +6616,10 @@ static EMIT_OPCODE emit_opcodelist[] = { { 95, "eq", emit_parm0 }, {106, "eq.c.alt", emit_parm1_any }, {105, "eq.c.pri", emit_parm1_any }, - {119, "fill", emit_parm1_any }, + {119, "fill", emit_parm1_nonneg }, {100, "geq", emit_parm0 }, { 99, "grtr", emit_parm0 }, - {120, "halt", emit_parm1_any }, + {120, "halt", emit_parm1_nonneg }, { 45, "heap", emit_parm1_any }, { 27, "idxaddr", emit_parm0 }, { 28, "idxaddr.b", emit_parm1_any }, @@ -6608,7 +6663,7 @@ static EMIT_OPCODE emit_opcodelist[] = { { 7, "lref.s.pri", emit_parm1_local }, { 34, "move.alt", emit_parm0 }, { 33, "move.pri", emit_parm0 }, - {117, "movs", emit_parm1_any }, + {117, "movs", emit_parm1_nonneg }, { 85, "neg", emit_parm0 }, { 96, "neq", emit_parm0 }, {134, "nop", emit_parm0 }, diff --git a/source/compiler/sc2.c b/source/compiler/sc2.c index 291d086..b32141a 100644 --- a/source/compiler/sc2.c +++ b/source/compiler/sc2.c @@ -2098,7 +2098,7 @@ char *sc_tokens[] = { ";", ";", "-integer value-", "-rational value-", "-identifier-", "-label-", "-string-", "-numeric value-", "-data offset-", "-local variable-", "-function-", - "-native function-" + "-native function-", "-nonnegative integer-" }; SC_FUNC int lex(cell *lexvalue,char **lexsym) From bc76324d03df8ffe7a0f87ab43a3021ff800e7ae Mon Sep 17 00:00:00 2001 From: Daniel_Cortez Date: Wed, 31 Jan 2018 07:46:27 +0700 Subject: [PATCH 04/15] emit: Check if the argument of 'shl.c.pri/alt' and 'shr.c.pri/alt' is in range from 0 to (sizeof(cell)*8-1) --- source/compiler/sc1.c | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/source/compiler/sc1.c b/source/compiler/sc1.c index 8132c18..3a773cb 100644 --- a/source/compiler/sc1.c +++ b/source/compiler/sc1.c @@ -6113,7 +6113,7 @@ static void SC_FASTCALL emit_param_local(emit_outval *p) } /* switch */ } -static void SC_FASTCALL emit_param_index(emit_outval *p,const cell *valid_values,int numvalues) +static void SC_FASTCALL emit_param_index(emit_outval *p,int isrange,const cell *valid_values,int numvalues) { cell val; char *str; @@ -6121,7 +6121,7 @@ static void SC_FASTCALL emit_param_index(emit_outval *p,const cell *valid_values int tok,i,global; int neg=FALSE; - assert(numvalues>0); + assert(isrange ? (numvalues==2) : (numvalues>0)); fetchtok: tok=lex(&val,&str); switch (tok) { @@ -6175,9 +6175,14 @@ fetchtok: p->type=eotNUMBER; p->value.ucell=(ucell)val; - for (i=0; i Date: Wed, 31 Jan 2018 19:15:10 +0700 Subject: [PATCH 05/15] emit: Allow the use of labels in arguments of type 'any' --- source/compiler/sc.h | 1 + source/compiler/sc1.c | 11 ++++++----- source/compiler/sc4.c | 3 +++ source/compiler/sc6.c | 8 +++++++- 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/source/compiler/sc.h b/source/compiler/sc.h index 46ff20a..e38b147 100644 --- a/source/compiler/sc.h +++ b/source/compiler/sc.h @@ -478,6 +478,7 @@ typedef enum s_optmark { #define eotNUMBER 0 #define eotFUNCTION 1 +#define eotLABEL 2 typedef struct s_emit_outval { int type; union { diff --git a/source/compiler/sc1.c b/source/compiler/sc1.c index 3a773cb..5d7ff97 100644 --- a/source/compiler/sc1.c +++ b/source/compiler/sc1.c @@ -5981,10 +5981,12 @@ fetchtok: break; } /* if */ if (sym->ident==iLABEL) { - tok=tLABEL; - goto invalid_token; - } /* if */ - if (sym->ident==iFUNCTN || sym->ident==iREFFUNC) { + if (neg!=FALSE) + goto invalid_token_neg; + sym->usage|=uREAD; + p->type=eotLABEL; + p->value.ucell=(ucell)sym->addr; + } else if (sym->ident==iFUNCTN || sym->ident==iREFFUNC) { if (neg!=FALSE) goto invalid_token_neg; if ((sym->usage & uNATIVE)!=0 && (sym->usage & uREAD)==0 && sym->addr>=0) @@ -6024,7 +6026,6 @@ fetchtok: break; } /* if */ default: - invalid_token: emit_invalid_token(teNUMERIC,tok); } /* switch */ } diff --git a/source/compiler/sc4.c b/source/compiler/sc4.c index b335ddb..410f7a9 100644 --- a/source/compiler/sc4.c +++ b/source/compiler/sc4.c @@ -1390,6 +1390,9 @@ SC_FUNC void outinstr(const char *name, emit_outval params[],int numparams) stgwrite(" "); switch (params[i].type) { + case eotLABEL: + stgwrite("l."); + /* fallthrough */ case eotNUMBER: stgwrite(itoh(params[i].value.ucell)); break; diff --git a/source/compiler/sc6.c b/source/compiler/sc6.c index 0216f41..21b5806 100644 --- a/source/compiler/sc6.c +++ b/source/compiler/sc6.c @@ -102,7 +102,7 @@ static ucell getparam(const char *s,char **n) char name[sNAMEMAX+1]; symbol *sym; - if (*s=='.') { + if (s[0]=='.') { /* this is a function, find it in the global symbol table */ for (i=0; !isspace(*(++s)); i++) { assert(*s!='\0'); @@ -115,6 +115,12 @@ static ucell getparam(const char *s,char **n) assert(sym->ident==iFUNCTN || sym->ident==iREFFUNC); assert(sym->vclass==sGLOBAL); result=sym->addr; + } else if (s[0]=='l' && s[1]=='.') { + /* this is a label */ + i=(int)hex2long(s+2,NULL); + assert(i>=0 && i Date: Thu, 1 Feb 2018 02:43:54 +0700 Subject: [PATCH 06/15] emit: Minor fixes/adjustments --- source/compiler/sc.h | 6 +++--- source/compiler/sc1.c | 41 +++++++++++++++++++++-------------------- source/compiler/sc6.c | 2 +- 3 files changed, 25 insertions(+), 24 deletions(-) diff --git a/source/compiler/sc.h b/source/compiler/sc.h index e38b147..15fa2da 100644 --- a/source/compiler/sc.h +++ b/source/compiler/sc.h @@ -476,9 +476,9 @@ typedef enum s_optmark { #define MAX_INSTR_LEN 30 -#define eotNUMBER 0 -#define eotFUNCTION 1 -#define eotLABEL 2 +#define eotNUMBER 0 +#define eotFUNCTION 1 +#define eotLABEL 2 typedef struct s_emit_outval { int type; union { diff --git a/source/compiler/sc1.c b/source/compiler/sc1.c index 5d7ff97..dea9010 100644 --- a/source/compiler/sc1.c +++ b/source/compiler/sc1.c @@ -137,7 +137,6 @@ static void dostate(void); static void addwhile(int *ptr); static void delwhile(void); static int *readwhile(void); -static void doemit(void); typedef void (SC_FASTCALL *OPCODE_PROC)(char *name); typedef struct { @@ -5295,7 +5294,7 @@ static void statement(int *lastindent,int allow_decl) lexclr(FALSE); tok=lex(&val,&st); } /* case */ - /* drop through */ + /* fallthrough */ default: /* non-empty expression */ sc_allowproccall=optproccall; lexpush(); /* analyze token later */ @@ -5959,18 +5958,18 @@ static void SC_FASTCALL emit_param_any(emit_outval *p) cell val,cidx; symbol *sym; extern char *sc_tokens[]; - int tok,neg,ident,index; + int tok,negate,ident,index; - neg=FALSE; + negate=FALSE; p->type=eotNUMBER; fetchtok: tok=lex(&val,&str); switch (tok) { case tNUMBER: - p->value.ucell=(ucell)((neg!=FALSE) ? -val : val); + p->value.ucell=(ucell)(negate ? -val : val); break; case tRATIONAL: - p->value.ucell=(ucell)((neg!=FALSE) ? (val|((cell)1 << (PAWN_CELL_SIZE-1))) : val); + p->value.ucell=(ucell)(negate ? (val|((cell)1 << (PAWN_CELL_SIZE-1))) : val); break; case tSYMBOL: sym=findloc(str); @@ -5981,13 +5980,13 @@ fetchtok: break; } /* if */ if (sym->ident==iLABEL) { - if (neg!=FALSE) + if (negate) goto invalid_token_neg; sym->usage|=uREAD; p->type=eotLABEL; p->value.ucell=(ucell)sym->addr; } else if (sym->ident==iFUNCTN || sym->ident==iREFFUNC) { - if (neg!=FALSE) + if (negate) goto invalid_token_neg; if ((sym->usage & uNATIVE)!=0 && (sym->usage & uREAD)==0 && sym->addr>=0) sym->addr=ntv_funcid++; @@ -5996,7 +5995,7 @@ fetchtok: p->value.string=str; } else { markusage(sym,uREAD | uWRITTEN); - p->value.ucell=(ucell)((neg!=FALSE) ? -sym->addr : sym->addr); + p->value.ucell=(ucell)(negate ? -sym->addr : sym->addr); } /* if */ break; case '(': @@ -6012,11 +6011,11 @@ fetchtok: if ((emit_flags & efEXPR)==0) stgset(FALSE); needtoken(')'); - p->value.ucell=(ucell)((neg!=FALSE) ? -val : val); + p->value.ucell=(ucell)(negate ? -val : val); break; case '-': - if (neg==FALSE) { - neg=TRUE; + if (!negate) { + negate=TRUE; goto fetchtok; } else { char ival[sNAMEMAX+2]; @@ -6120,18 +6119,18 @@ static void SC_FASTCALL emit_param_index(emit_outval *p,int isrange,const cell * char *str; symbol *sym; int tok,i,global; - int neg=FALSE; + int negate=FALSE; assert(isrange ? (numvalues==2) : (numvalues>0)); fetchtok: tok=lex(&val,&str); switch (tok) { case tNUMBER: - if (neg!=FALSE) + if (negate) val=-val; break; case tRATIONAL: - if (neg!=FALSE) + if (negate) val=val|((cell)1 << (PAWN_CELL_SIZE-1)); break; case tSYMBOL: @@ -6160,14 +6159,14 @@ fetchtok: goto invalid_token; } /* switch */ markusage(sym,uREAD); - val=(neg!=FALSE) ? -sym->addr : sym->addr; + val=negate ? -sym->addr : sym->addr; break; case '-': - if (neg==FALSE) { - neg=TRUE; + if (!negate) { + negate=TRUE; goto fetchtok; } /* if */ - /* drop through */ + /* fallthrough */ default: invalid_token: emit_invalid_token(teNUMERIC,tok); @@ -6224,6 +6223,8 @@ static void SC_FASTCALL emit_param_nonneg(emit_outval *p) goto invalid_token; } /* switch */ markusage(sym,uREAD); + val=sym->addr; + break; default: invalid_token: emit_invalid_token(teNONNEG,tok); @@ -6273,7 +6274,7 @@ static void SC_FASTCALL emit_param_function(emit_outval *p,int isnative) } else { tok=(sym->ident==iCONSTEXPR) ? teNUMERIC : teDATA; } /* if */ - /* drop through */ + /* fallthrough */ default: emit_invalid_token((isnative!=FALSE) ? teNATIVE : teFUNCTN,tok); return; diff --git a/source/compiler/sc6.c b/source/compiler/sc6.c index 21b5806..06e979e 100644 --- a/source/compiler/sc6.c +++ b/source/compiler/sc6.c @@ -444,7 +444,7 @@ static cell SC_FASTCALL do_dumpn(FILE *fbin,char *params,cell opcode) value=hex2long(params,¶ms); num=(int)hex2long(params,NULL); if (fbin!=NULL) - write_encoded_n(fbin,value,num); + write_encoded_n(fbin,value,num); return num*sizeof(cell); } From 7de402d997e81c5ef4a93cd8fcdf2861aac9e51c Mon Sep 17 00:00:00 2001 From: Daniel_Cortez Date: Sun, 4 Feb 2018 00:48:55 +0700 Subject: [PATCH 07/15] emit: Fix "extra characters on line" error on single-line 'emit{}' statements --- source/compiler/sc1.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/source/compiler/sc1.c b/source/compiler/sc1.c index dea9010..ef76056 100644 --- a/source/compiler/sc1.c +++ b/source/compiler/sc1.c @@ -5285,7 +5285,6 @@ static void statement(int *lastindent,int allow_decl) extern char *sc_tokens[]; const unsigned char *bck_lptr=lptr-strlen(sc_tokens[tok-tFIRST]); if (matchtoken('{')) { - lexpush(); emit_flags |= efBLOCK; lastst=tEMIT; break; @@ -6808,7 +6807,7 @@ SC_FUNC void emit_parse_line(void) * and copy the instruction name */ lptr-=len; - for(i=0; i Date: Sun, 4 Feb 2018 18:20:12 +0700 Subject: [PATCH 08/15] emit: Add optional ':' prefix for labels as instruction arguments --- source/compiler/sc1.c | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/source/compiler/sc1.c b/source/compiler/sc1.c index ef76056..1b52149 100644 --- a/source/compiler/sc1.c +++ b/source/compiler/sc1.c @@ -6012,6 +6012,15 @@ fetchtok: needtoken(')'); p->value.ucell=(ucell)(negate ? -val : val); break; + case ':': + tok=lex(&val,&str); + if (tok!=tSYMBOL) + emit_invalid_token(tSYMBOL,tok); + sym=fetchlab(str); + sym->usage|=uREAD; + p->type=eotLABEL; + p->value.ucell=(ucell)sym->addr; + break; case '-': if (!negate) { negate=TRUE; @@ -6242,12 +6251,23 @@ static void SC_FASTCALL emit_param_label(emit_outval *p) int tok; tok=lex(&val,&str); - if (tok!=tSYMBOL) - emit_invalid_token(tSYMBOL,tok); - sym=fetchlab(str); - sym->usage|=uREAD; - p->type=eotNUMBER; - p->value.ucell=(ucell)sym->addr; + switch (tok) + { + case ':': + tok=lex(&val,&str); + if (tok!=tSYMBOL) + emit_invalid_token(tSYMBOL,tok); + /* fallthrough */ + case tSYMBOL: + sym=fetchlab(str); + sym->usage|=uREAD; + p->type=eotNUMBER; + p->value.ucell=(ucell)sym->addr; + break; + default: + invalid_token: + emit_invalid_token(tSYMBOL, tok); + } } static void SC_FASTCALL emit_param_function(emit_outval *p,int isnative) From eb4f6ea6249007bba314967df981c4ce65d379dd Mon Sep 17 00:00:00 2001 From: Daniel_Cortez Date: Wed, 7 Feb 2018 17:47:12 +0700 Subject: [PATCH 09/15] emit: Remove duplicate code from 'emit_param_index()' and 'emit_param_nonneg()', reuse the code from 'emit_param_any()' instead --- source/compiler/sc1.c | 209 ++++++++++++++++-------------------------- 1 file changed, 79 insertions(+), 130 deletions(-) diff --git a/source/compiler/sc1.c b/source/compiler/sc1.c index 1b52149..ddf5377 100644 --- a/source/compiler/sc1.c +++ b/source/compiler/sc1.c @@ -5951,7 +5951,8 @@ static void SC_FASTCALL emit_invalid_token(int expected_token,int found_token) } /* if */ } -static void SC_FASTCALL emit_param_any(emit_outval *p) +static int SC_FASTCALL emit_param_any_internal(emit_outval *p,int expected_tok, + int allow_nonint,int allow_expr) { char *str; cell val,cidx; @@ -5968,6 +5969,8 @@ fetchtok: p->value.ucell=(ucell)(negate ? -val : val); break; case tRATIONAL: + if (!allow_nonint) + goto invalid_token; p->value.ucell=(ucell)(negate ? (val|((cell)1 << (PAWN_CELL_SIZE-1))) : val); break; case tSYMBOL: @@ -5976,46 +5979,64 @@ fetchtok: sym=findglb(str,sSTATEVAR); if (sym==NULL || (sym->ident!=iFUNCTN && sym->ident!=iREFFUNC && (sym->usage & uDEFINE)==0)) { error(17,str); /* undefined symbol */ - break; + return FALSE; } /* if */ if (sym->ident==iLABEL) { if (negate) goto invalid_token_neg; + if (!allow_nonint) { + tok=tLABEL; + goto invalid_token; + } /* if */ sym->usage|=uREAD; p->type=eotLABEL; p->value.ucell=(ucell)sym->addr; } else if (sym->ident==iFUNCTN || sym->ident==iREFFUNC) { if (negate) goto invalid_token_neg; + if (!allow_nonint) { + tok=(sym->usage & uNATIVE) ? teNATIVE : teFUNCTN; + goto invalid_token; + } /* if */ if ((sym->usage & uNATIVE)!=0 && (sym->usage & uREAD)==0 && sym->addr>=0) sym->addr=ntv_funcid++; markusage(sym,uREAD); p->type=eotFUNCTION; p->value.string=str; } else { + if (!allow_nonint && sym->ident!=iCONSTEXPR) { + tok=(sym->vclass==sLOCAL) ? teLOCAL : teDATA; + goto invalid_token; + } /* if */ markusage(sym,uREAD | uWRITTEN); p->value.ucell=(ucell)(negate ? -sym->addr : sym->addr); } /* if */ break; case '(': + if (!allow_expr) + goto invalid_token; if ((emit_flags & efEXPR)==0) stgset(TRUE); stgget(&index,&cidx); errorset(sEXPRMARK,0); ident=expression(&val,NULL,NULL,FALSE); - if (ident!=iCONSTEXPR) - error(8); /* must be constant expression */ errorset(sEXPRRELEASE,0); stgdel(index,cidx); if ((emit_flags & efEXPR)==0) stgset(FALSE); needtoken(')'); p->value.ucell=(ucell)(negate ? -val : val); + if (ident!=iCONSTEXPR) { + error(8); /* must be constant expression */ + return FALSE; + } /* if */ break; case ':': tok=lex(&val,&str); - if (tok!=tSYMBOL) + if (tok!=tSYMBOL) { emit_invalid_token(tSYMBOL,tok); + return FALSE; + } /* if */ sym=fetchlab(str); sym->usage|=uREAD; p->type=eotLABEL; @@ -6029,12 +6050,62 @@ fetchtok: char ival[sNAMEMAX+2]; invalid_token_neg: sprintf(ival,"-%s",str); - error(1,sc_tokens[teNUMERIC-tFIRST],ival); - break; + error(1,sc_tokens[expected_tok-tFIRST],ival); + return FALSE; } /* if */ default: - emit_invalid_token(teNUMERIC,tok); + invalid_token: + emit_invalid_token(expected_tok,tok); + return FALSE; } /* switch */ + return TRUE; +} + +static void SC_FASTCALL emit_param_any(emit_outval *p) +{ + emit_param_any_internal(p,teNUMERIC,TRUE,TRUE); +} + +static void SC_FASTCALL emit_param_index(emit_outval *p,int isrange,const cell *valid_values,int numvalues) +{ + int i; + cell val; + + assert(isrange ? (numvalues==2) : (numvalues>0)); + if (!emit_param_any_internal(p,tNUMBER,FALSE,FALSE)) + return; + val=(cell)p->value.ucell; + if (isrange) { + if (valid_values[0]<=val && val<=valid_values[1]) + return; + } else { + for (i=0; ivalue.ucell<(cell)0) { + extern char *sc_tokens[]; +#if PAWN_CELL_SIZE==16 + char ival[7]; + sprintf(ival,"%ld",(long)p->value.ucell); +#elif PAWN_CELL_SIZE==32 + char ival[12]; + sprintf(ival,"%ld",(long)p->value.ucell); +#elif PAWN_CELL_SIZE==64 + char ival[21]; + sprintf(ival,"%lld",(long long)p->value.ucell); +#else + #error Unsupported cell size +#endif + error(1,sc_tokens[teNONNEG-tFIRST],ival); + } /* if */ } static void SC_FASTCALL emit_param_data(emit_outval *p) @@ -6121,128 +6192,6 @@ static void SC_FASTCALL emit_param_local(emit_outval *p) } /* switch */ } -static void SC_FASTCALL emit_param_index(emit_outval *p,int isrange,const cell *valid_values,int numvalues) -{ - cell val; - char *str; - symbol *sym; - int tok,i,global; - int negate=FALSE; - - assert(isrange ? (numvalues==2) : (numvalues>0)); -fetchtok: - tok=lex(&val,&str); - switch (tok) { - case tNUMBER: - if (negate) - val=-val; - break; - case tRATIONAL: - if (negate) - val=val|((cell)1 << (PAWN_CELL_SIZE-1)); - break; - case tSYMBOL: - global=FALSE; - sym=findloc(str); - if (sym==NULL) { - global=TRUE; - sym=findglb(str,sSTATEVAR); - } /* if */ - if (sym==NULL) { - error(17,str); /* undefined symbol */ - return; - } /* if */ - switch (sym->ident) { - case iCONSTEXPR: - break; - case iLABEL: - tok=tLABEL; - goto invalid_token; - case iFUNCTN: - case iREFFUNC: - tok=((sym->usage & uNATIVE)!=0) ? teNATIVE : teFUNCTN; - goto invalid_token; - default: - tok=(global==FALSE && sym->vclass!=sSTATIC) ? teLOCAL : teDATA; - goto invalid_token; - } /* switch */ - markusage(sym,uREAD); - val=negate ? -sym->addr : sym->addr; - break; - case '-': - if (!negate) { - negate=TRUE; - goto fetchtok; - } /* if */ - /* fallthrough */ - default: - invalid_token: - emit_invalid_token(teNUMERIC,tok); - return; - } /* switch */ - - p->type=eotNUMBER; - p->value.ucell=(ucell)val; - if (isrange) { - if (valid_values[0]<=val && val<=valid_values[1]) - return; - } else { - for (i=0; iident) { - case iCONSTEXPR: - break; - case iLABEL: - tok=tLABEL; - goto invalid_token; - case iFUNCTN: - case iREFFUNC: - tok=((sym->usage & uNATIVE)!=0) ? teNATIVE : teFUNCTN; - goto invalid_token; - default: - tok=(global==FALSE && sym->vclass!=sSTATIC) ? teLOCAL : teDATA; - goto invalid_token; - } /* switch */ - markusage(sym,uREAD); - val=sym->addr; - break; - default: - invalid_token: - emit_invalid_token(teNONNEG,tok); - return; - } /* switch */ - - p->type=eotNUMBER; - p->value.ucell=(ucell)val; -} - static void SC_FASTCALL emit_param_label(emit_outval *p) { cell val; From 3c252211f0453ce4cf59c76c6b84c6ddfaa2cb65 Mon Sep 17 00:00:00 2001 From: Daniel_Cortez Date: Wed, 7 Feb 2018 22:42:49 +0700 Subject: [PATCH 10/15] emit: Minor adjustments --- source/compiler/sc.h | 2 +- source/compiler/sc1.c | 65 +++++++++++++++++++++++-------------------- source/compiler/sc4.c | 2 +- 3 files changed, 37 insertions(+), 32 deletions(-) diff --git a/source/compiler/sc.h b/source/compiler/sc.h index 15fa2da..f1382e7 100644 --- a/source/compiler/sc.h +++ b/source/compiler/sc.h @@ -711,7 +711,7 @@ SC_FUNC void dec(value *lval); SC_FUNC void jmp_ne0(int number); SC_FUNC void jmp_eq0(int number); SC_FUNC void outval(cell val,int newline); -SC_FUNC void outinstr(const char *name, emit_outval params[],int numparams); +SC_FUNC void outinstr(const char *name,emit_outval params[],int numparams); /* function prototypes in SC5.C */ SC_FUNC int error(int number,...); diff --git a/source/compiler/sc1.c b/source/compiler/sc1.c index ddf5377..44b33ab 100644 --- a/source/compiler/sc1.c +++ b/source/compiler/sc1.c @@ -5957,7 +5957,6 @@ static int SC_FASTCALL emit_param_any_internal(emit_outval *p,int expected_tok, char *str; cell val,cidx; symbol *sym; - extern char *sc_tokens[]; int tok,negate,ident,index; negate=FALSE; @@ -5982,16 +5981,17 @@ fetchtok: return FALSE; } /* if */ if (sym->ident==iLABEL) { + sym->usage|=uREAD; if (negate) goto invalid_token_neg; if (!allow_nonint) { tok=tLABEL; goto invalid_token; } /* if */ - sym->usage|=uREAD; p->type=eotLABEL; p->value.ucell=(ucell)sym->addr; } else if (sym->ident==iFUNCTN || sym->ident==iREFFUNC) { + markusage(sym,uREAD); if (negate) goto invalid_token_neg; if (!allow_nonint) { @@ -6000,15 +6000,14 @@ fetchtok: } /* if */ if ((sym->usage & uNATIVE)!=0 && (sym->usage & uREAD)==0 && sym->addr>=0) sym->addr=ntv_funcid++; - markusage(sym,uREAD); p->type=eotFUNCTION; p->value.string=str; } else { + markusage(sym,uREAD | uWRITTEN); if (!allow_nonint && sym->ident!=iCONSTEXPR) { tok=(sym->vclass==sLOCAL) ? teLOCAL : teDATA; goto invalid_token; } /* if */ - markusage(sym,uREAD | uWRITTEN); p->value.ucell=(ucell)(negate ? -sym->addr : sym->addr); } /* if */ break; @@ -6047,6 +6046,7 @@ fetchtok: negate=TRUE; goto fetchtok; } else { + extern char *sc_tokens[]; char ival[sNAMEMAX+2]; invalid_token_neg: sprintf(ival,"-%s",str); @@ -6124,6 +6124,7 @@ static void SC_FASTCALL emit_param_data(emit_outval *p) case tSYMBOL: sym=findloc(str); if (sym!=NULL) { + markusage(sym,uREAD | uWRITTEN); if (sym->ident==iLABEL) { tok=tLABEL; goto invalid_token; @@ -6138,12 +6139,12 @@ static void SC_FASTCALL emit_param_data(emit_outval *p) error(17,str); /* undefined symbol */ break; } /* if */ + markusage(sym,uREAD | uWRITTEN); if (sym->ident==iFUNCTN || sym->ident==iREFFUNC) { tok=((sym->usage & uNATIVE)!=0) ? teNATIVE : teFUNCTN; goto invalid_token; } /* if */ } /* if */ - markusage(sym,uREAD | uWRITTEN); p->value.ucell=(ucell)sym->addr; break; default: @@ -6168,6 +6169,7 @@ static void SC_FASTCALL emit_param_local(emit_outval *p) case tSYMBOL: sym=findloc(str); if (sym!=NULL) { + markusage(sym,uREAD | uWRITTEN); if (sym->ident==iLABEL) { tok=tLABEL; goto invalid_token; @@ -6178,12 +6180,15 @@ static void SC_FASTCALL emit_param_local(emit_outval *p) } /* if */ } else { sym=findglb(str,sSTATEVAR); - if (sym==NULL || sym->ident!=iCONSTEXPR) { + if (sym==NULL) { + undefined_sym: error(17,str); /* undefined symbol */ break; } /* if */ + markusage(sym,uREAD | uWRITTEN); + if (sym->ident!=iCONSTEXPR) + goto undefined_sym; } /* if */ - markusage(sym,uREAD | uWRITTEN); p->value.ucell=(ucell)sym->addr; break; default: @@ -6199,23 +6204,23 @@ static void SC_FASTCALL emit_param_label(emit_outval *p) symbol *sym; int tok; + p->type=eotNUMBER; tok=lex(&val,&str); switch (tok) { case ':': tok=lex(&val,&str); if (tok!=tSYMBOL) - emit_invalid_token(tSYMBOL,tok); + goto invalid_token; /* fallthrough */ case tSYMBOL: sym=fetchlab(str); sym->usage|=uREAD; - p->type=eotNUMBER; p->value.ucell=(ucell)sym->addr; break; default: invalid_token: - emit_invalid_token(tSYMBOL, tok); + emit_invalid_token(tSYMBOL,tok); } } @@ -6226,6 +6231,7 @@ static void SC_FASTCALL emit_param_function(emit_outval *p,int isnative) symbol *sym; int tok; + p->type=eotNUMBER; tok=lex(&val,&str); switch (tok) { @@ -6235,6 +6241,7 @@ static void SC_FASTCALL emit_param_function(emit_outval *p,int isnative) error(17,str); /* undefined symbol */ return; } /* if */ + markusage(sym,uREAD); if (sym->ident==iFUNCTN || sym->ident==iREFFUNC) { if (!!(sym->usage & uNATIVE)==isnative) break; @@ -6251,13 +6258,11 @@ static void SC_FASTCALL emit_param_function(emit_outval *p,int isnative) if (isnative!=FALSE) { if ((sym->usage & uREAD)==0 && sym->addr>=0) sym->addr=ntv_funcid++; - p->type=eotNUMBER; p->value.ucell=(ucell)sym->addr; } else { p->type=eotFUNCTION; p->value.string=str; } /* if */ - markusage(sym,uREAD); } static void SC_FASTCALL emit_noop(char *name) @@ -6278,6 +6283,23 @@ static void SC_FASTCALL emit_parm1_any(char *name) outinstr(name,p,(sizeof p / sizeof p[0])); } +static void SC_FASTCALL emit_parm1_nonneg(char *name) +{ + emit_outval p[1]; + + emit_param_nonneg(&p[0]); + outinstr(name,p,(sizeof p / sizeof p[0])); +} + +static void SC_FASTCALL emit_parm1_shift(char *name) +{ + static const cell valid_values[] = { 0,sizeof(cell)*8-1 }; + emit_outval p[1]; + + emit_param_index(&p[0],TRUE,valid_values,(sizeof valid_values / sizeof valid_values[0])); + outinstr(name,p,(sizeof p / sizeof p[0])); +} + static void SC_FASTCALL emit_parm1_data(char *name) { emit_outval p[1]; @@ -6302,23 +6324,6 @@ static void SC_FASTCALL emit_parm1_label(char *name) outinstr(name,p,(sizeof p / sizeof p[0])); } -static void SC_FASTCALL emit_parm1_nonneg(char *name) -{ - emit_outval p[1]; - - emit_param_nonneg(&p[0]); - outinstr(name,p,(sizeof p / sizeof p[0])); -} - -static void SC_FASTCALL emit_parm1_shift(char *name) -{ - static const cell valid_values[] = { 0,sizeof(cell)*8-1 }; - emit_outval p[1]; - - emit_param_index(&p[0],TRUE,valid_values,(sizeof valid_values / sizeof valid_values[0])); - outinstr(name,p,(sizeof p / sizeof p[0])); -} - static void SC_FASTCALL emit_do_casetbl(char *name) { emit_outval p[2]; @@ -6757,12 +6762,12 @@ static int emit_findopcode(const char *instr,int maxlen) SC_FUNC void emit_parse_line(void) { + extern char *sc_tokens[]; cell val; char* st; int tok,len,i; symbol *sym; char name[MAX_INSTR_LEN]; - extern char *sc_tokens[]; tok=tokeninfo(&val,&st); if (tok==tSYMBOL || (tok>tMIDDLE && tok<=tLAST)) { diff --git a/source/compiler/sc4.c b/source/compiler/sc4.c index 410f7a9..8a39ab9 100644 --- a/source/compiler/sc4.c +++ b/source/compiler/sc4.c @@ -1379,7 +1379,7 @@ SC_FUNC void outval(cell val,int newline) } /* write an instruction with arguments */ -SC_FUNC void outinstr(const char *name, emit_outval params[],int numparams) +SC_FUNC void outinstr(const char *name,emit_outval params[],int numparams) { int i; From 7a0e966edf3483beb67f8b7cac44f1a259cc45e0 Mon Sep 17 00:00:00 2001 From: Daniel_Cortez Date: Thu, 8 Feb 2018 00:30:06 +0700 Subject: [PATCH 11/15] emit: Add new argument type: 'integer value' --- source/compiler/sc1.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/source/compiler/sc1.c b/source/compiler/sc1.c index 44b33ab..0d89c7f 100644 --- a/source/compiler/sc1.c +++ b/source/compiler/sc1.c @@ -6066,6 +6066,11 @@ static void SC_FASTCALL emit_param_any(emit_outval *p) emit_param_any_internal(p,teNUMERIC,TRUE,TRUE); } +static void SC_FASTCALL emit_param_integer(emit_outval *p) +{ + emit_param_any_internal(p,teNUMERIC,FALSE,TRUE); +} + static void SC_FASTCALL emit_param_index(emit_outval *p,int isrange,const cell *valid_values,int numvalues) { int i; @@ -6283,6 +6288,14 @@ static void SC_FASTCALL emit_parm1_any(char *name) outinstr(name,p,(sizeof p / sizeof p[0])); } +static void SC_FASTCALL emit_parm1_integer(char *name) +{ + emit_outval p[1]; + + emit_param_integer(&p[0]); + outinstr(name,p,(sizeof p / sizeof p[0])); +} + static void SC_FASTCALL emit_parm1_nonneg(char *name) { emit_outval p[1]; @@ -6586,7 +6599,7 @@ static EMIT_OPCODE emit_opcodelist[] = { { 30, "align.alt", emit_parm1_any }, { 29, "align.pri", emit_parm1_any }, { 81, "and", emit_parm0 }, - {121, "bounds", emit_parm1_nonneg }, + {121, "bounds", emit_parm1_integer }, {137, "break", emit_parm0 }, { 49, "call", emit_do_call }, { 50, "call.pri", emit_parm0 }, @@ -6609,7 +6622,7 @@ static EMIT_OPCODE emit_opcodelist[] = { {100, "geq", emit_parm0 }, { 99, "grtr", emit_parm0 }, {120, "halt", emit_parm1_nonneg }, - { 45, "heap", emit_parm1_any }, + { 45, "heap", emit_parm1_integer }, { 27, "idxaddr", emit_parm0 }, { 28, "idxaddr.b", emit_parm1_any }, {109, "inc", emit_parm1_data }, @@ -6666,7 +6679,7 @@ static EMIT_OPCODE emit_opcodelist[] = { { 37, "push.alt", emit_parm0 }, { 39, "push.c", emit_parm1_any }, { 36, "push.pri", emit_parm0 }, - { 38, "push.r", emit_parm1_any }, + { 38, "push.r", emit_parm1_integer }, { 41, "push.s", emit_parm1_local }, {139, "push2", emit_do_pushn }, {141, "push2.adr", emit_do_pushn_s_adr }, @@ -6702,13 +6715,13 @@ static EMIT_OPCODE emit_opcodelist[] = { {102, "sleq", emit_parm0 }, {101, "sless", emit_parm0 }, { 72, "smul", emit_parm0 }, - { 88, "smul.c", emit_parm1_any }, + { 88, "smul.c", emit_parm1_integer }, { 20, "sref.alt", emit_parm1_data }, { 19, "sref.pri", emit_parm1_data }, { 22, "sref.s.alt", emit_parm1_local }, { 21, "sref.s.pri", emit_parm1_local }, { 67, "sshr", emit_parm0 }, - { 44, "stack", emit_parm1_any }, + { 44, "stack", emit_parm1_integer }, { 16, "stor.alt", emit_parm1_data }, { 23, "stor.i", emit_parm0 }, { 15, "stor.pri", emit_parm1_data }, From 78f69596300e5fd14bbf453875a7c7d50da6dfad Mon Sep 17 00:00:00 2001 From: Daniel_Cortez Date: Thu, 8 Feb 2018 00:50:05 +0700 Subject: [PATCH 12/15] emit: Change the argument type in 'lidx.b' and 'idxaddr.b' to 'shift' --- source/compiler/sc1.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/compiler/sc1.c b/source/compiler/sc1.c index 0d89c7f..93a2d2f 100644 --- a/source/compiler/sc1.c +++ b/source/compiler/sc1.c @@ -6624,7 +6624,7 @@ static EMIT_OPCODE emit_opcodelist[] = { {120, "halt", emit_parm1_nonneg }, { 45, "heap", emit_parm1_integer }, { 27, "idxaddr", emit_parm0 }, - { 28, "idxaddr.b", emit_parm1_any }, + { 28, "idxaddr.b", emit_parm1_shift }, {109, "inc", emit_parm1_data }, {108, "inc.alt", emit_parm0 }, {111, "inc.i", emit_parm0 }, @@ -6650,7 +6650,7 @@ static EMIT_OPCODE emit_opcodelist[] = { { 98, "leq", emit_parm0 }, { 97, "less", emit_parm0 }, { 25, "lidx", emit_parm0 }, - { 26, "lidx.b", emit_parm1_any }, + { 26, "lidx.b", emit_parm1_shift }, { 2, "load.alt", emit_parm1_data }, {154, "load.both", emit_do_load_both }, { 9, "load.i", emit_parm0 }, From b0026d8ce65028e19d15d58ddea0c7af00765a58 Mon Sep 17 00:00:00 2001 From: Daniel_Cortez Date: Sun, 11 Feb 2018 20:54:51 +0700 Subject: [PATCH 13/15] emit: Check if the argument of 'align.pri/alt' is in range from 0 to (sizeof(cell)-1) --- source/compiler/sc1.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/source/compiler/sc1.c b/source/compiler/sc1.c index 93a2d2f..432c997 100644 --- a/source/compiler/sc1.c +++ b/source/compiler/sc1.c @@ -6367,6 +6367,15 @@ static void SC_FASTCALL emit_do_lodb_strb(char *name) outinstr(name,p,(sizeof p / sizeof p[0])); } +static void SC_FASTCALL emit_do_align(char *name) +{ + static const cell valid_values[] = { 0,sizeof(cell)-1 }; + emit_outval p[1]; + + emit_param_index(&p[0],TRUE,valid_values,(sizeof valid_values / sizeof valid_values[0])); + outinstr(name,p,(sizeof p / sizeof p[0])); +} + static void SC_FASTCALL emit_do_lctrl(char *name) { static const cell valid_values[] = { 0,9 }; @@ -6596,8 +6605,8 @@ static EMIT_OPCODE emit_opcodelist[] = { { 87, "add.c", emit_parm1_any }, { 14, "addr.alt", emit_parm1_local }, { 13, "addr.pri", emit_parm1_local }, - { 30, "align.alt", emit_parm1_any }, - { 29, "align.pri", emit_parm1_any }, + { 30, "align.alt", emit_do_align }, + { 29, "align.pri", emit_do_align }, { 81, "and", emit_parm0 }, {121, "bounds", emit_parm1_integer }, {137, "break", emit_parm0 }, @@ -6751,10 +6760,7 @@ static int emit_findopcode(const char *instr,int maxlen) { int low,high,mid,cmp; - /* look up the instruction with a binary search - * the assembler is case insensitive to instructions (but case sensitive - * to symbols) - */ + /* look up the instruction with a binary search */ low=1; /* entry 0 is reserved (for "not found") */ high=(sizeof emit_opcodelist / sizeof emit_opcodelist[0])-1; while (low Date: Sun, 11 Feb 2018 23:15:26 +0700 Subject: [PATCH 14/15] emit: Make the compiler tell it expects "any value" for arguments of type 'any' --- source/compiler/sc.h | 17 +++++++++-------- source/compiler/sc1.c | 2 +- source/compiler/sc2.c | 4 ++-- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/source/compiler/sc.h b/source/compiler/sc.h index f1382e7..92ba3c2 100644 --- a/source/compiler/sc.h +++ b/source/compiler/sc.h @@ -398,15 +398,16 @@ typedef struct s_valuepair { #define tLABEL 337 #define tSTRING 338 /* argument types for emit/__emit */ -#define teNUMERIC 339 /* integer/rational number */ -#define teDATA 340 /* data (variable name or address) */ -#define teLOCAL 341 /* local variable (name or offset) */ -#define teFUNCTN 342 /* Pawn function */ -#define teNATIVE 343 /* native function */ -#define teNONNEG 344 /* nonnegative integer */ +#define teANY 339 /* any value */ +#define teNUMERIC 340 /* integer/rational number */ +#define teDATA 341 /* data (variable name or address) */ +#define teLOCAL 342 /* local variable (name or offset) */ +#define teFUNCTN 343 /* Pawn function */ +#define teNATIVE 344 /* native function */ +#define teNONNEG 345 /* nonnegative integer */ /* for assigment to "lastst" only (see SC1.C) */ -#define tEXPR 345 -#define tENDLESS 346 /* endless loop */ +#define tEXPR 346 +#define tENDLESS 347 /* endless loop */ /* (reversed) evaluation of staging buffer */ #define sSTARTREORDER 0x01 diff --git a/source/compiler/sc1.c b/source/compiler/sc1.c index 432c997..602d8c7 100644 --- a/source/compiler/sc1.c +++ b/source/compiler/sc1.c @@ -6063,7 +6063,7 @@ fetchtok: static void SC_FASTCALL emit_param_any(emit_outval *p) { - emit_param_any_internal(p,teNUMERIC,TRUE,TRUE); + emit_param_any_internal(p,teANY,TRUE,TRUE); } static void SC_FASTCALL emit_param_integer(emit_outval *p) diff --git a/source/compiler/sc2.c b/source/compiler/sc2.c index b32141a..66b84d7 100644 --- a/source/compiler/sc2.c +++ b/source/compiler/sc2.c @@ -2097,8 +2097,8 @@ char *sc_tokens[] = { "#tryinclude", "#undef", "#warning", ";", ";", "-integer value-", "-rational value-", "-identifier-", "-label-", "-string-", - "-numeric value-", "-data offset-", "-local variable-", "-function-", - "-native function-", "-nonnegative integer-" + "-any value-", "-numeric value-", "-data offset-", "-local variable-", + "-function-", "-native function-", "-nonnegative integer-" }; SC_FUNC int lex(cell *lexvalue,char **lexsym) From 900043e6950855b177d2718abaffd0a8c4663fd0 Mon Sep 17 00:00:00 2001 From: Daniel_Cortez Date: Mon, 12 Feb 2018 02:30:21 +0700 Subject: [PATCH 15/15] emit: Apply 'integer value' type to the argument of 'jrel' --- source/compiler/sc1.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/compiler/sc1.c b/source/compiler/sc1.c index 602d8c7..2ae98f4 100644 --- a/source/compiler/sc1.c +++ b/source/compiler/sc1.c @@ -6068,7 +6068,7 @@ static void SC_FASTCALL emit_param_any(emit_outval *p) static void SC_FASTCALL emit_param_integer(emit_outval *p) { - emit_param_any_internal(p,teNUMERIC,FALSE,TRUE); + emit_param_any_internal(p,tNUMBER,FALSE,TRUE); } static void SC_FASTCALL emit_param_index(emit_outval *p,int isrange,const cell *valid_values,int numvalues) @@ -6647,7 +6647,7 @@ static EMIT_OPCODE emit_opcodelist[] = { { 57, "jless", emit_parm1_label }, { 56, "jneq", emit_parm1_label }, { 54, "jnz", emit_parm1_label }, - { 52, "jrel", emit_parm1_any }, + { 52, "jrel", emit_parm1_integer }, { 64, "jsgeq", emit_parm1_label }, { 63, "jsgrtr", emit_parm1_label }, { 62, "jsleq", emit_parm1_label },