Fix conditional compilation not skipping #emit directives

#emit directives inside #if ... #endif blocks were processed by the
compiler regardless of the condition.

--------- test code --------

main() {
	#if 0
		#emit halt 1
	#endif
}

----- end of test code -----
This commit is contained in:
Zeex 2014-08-31 12:18:37 +07:00
parent e0a61e5f93
commit 6aa5ae3b00

View File

@ -1228,72 +1228,74 @@ static int command(void)
break; break;
#if !defined NOEMIT #if !defined NOEMIT
case tpEMIT: { case tpEMIT: {
/* write opcode to output file */ if (!SKIPPING) {
char name[40]; /* write opcode to output file */
int i; char name[40];
while (*lptr<=' ' && *lptr!='\0') int i;
lptr++; while (*lptr<=' ' && *lptr!='\0')
for (i=0; i<40 && (isalpha(*lptr) || *lptr=='.'); i++,lptr++) lptr++;
name[i]=(char)tolower(*lptr); for (i=0; i<40 && (isalpha(*lptr) || *lptr=='.'); i++,lptr++)
name[i]='\0'; name[i]=(char)tolower(*lptr);
stgwrite("\t"); name[i]='\0';
stgwrite(name); stgwrite("\t");
stgwrite(" "); stgwrite(name);
code_idx+=opcodes(1); stgwrite(" ");
/* write parameter (if any) */ code_idx+=opcodes(1);
while (*lptr<=' ' && *lptr!='\0') /* write parameter (if any) */
lptr++; while (*lptr<=' ' && *lptr!='\0')
if (*lptr!='\0') { lptr++;
symbol *sym; if (*lptr!='\0') {
tok=lex(&val,&str); symbol *sym;
switch (tok) { tok=lex(&val,&str);
case tNUMBER: switch (tok) {
case tRATIONAL: case tNUMBER:
outval(val,FALSE); case tRATIONAL:
code_idx+=opargs(1); outval(val,FALSE);
break;
case tSYMBOL:
sym=findloc(str);
if (sym==NULL)
sym=findglb(str,sSTATEVAR);
if (sym==NULL || sym->ident!=iFUNCTN && sym->ident!=iREFFUNC && (sym->usage & uDEFINE)==0) {
error(17,str); /* undefined symbol */
} else {
if (sym->ident==iFUNCTN || sym->ident==iREFFUNC) {
if ((sym->usage & uNATIVE)!=0) {
/* reserve a SYSREQ id if called for the first time */
if (sc_status==statWRITE && (sym->usage & uREAD)==0 && sym->addr>=0)
sym->addr=ntv_funcid++;
outval(sym->addr,FALSE);
} else {
/* normal function, write its name instead of the address
* so that the address will be resolved at assemble time
*/
stgwrite(".");
stgwrite(sym->name);
} /* if */
} else {
outval(sym->addr,FALSE);
} /* if */
/* mark symbol as "used", unknown whether for read or write */
markusage(sym,uREAD | uWRITTEN);
code_idx+=opargs(1); code_idx+=opargs(1);
} /* if */ break;
break; case tSYMBOL:
default: { sym=findloc(str);
char s2[20]; if (sym==NULL)
extern char *sc_tokens[];/* forward declaration */ sym=findglb(str,sSTATEVAR);
if (tok<256) if (sym==NULL || sym->ident!=iFUNCTN && sym->ident!=iREFFUNC && (sym->usage & uDEFINE)==0) {
sprintf(s2,"%c",(char)tok); error(17,str); /* undefined symbol */
else } else {
strcpy(s2,sc_tokens[tok-tFIRST]); if (sym->ident==iFUNCTN || sym->ident==iREFFUNC) {
error(1,sc_tokens[tSYMBOL-tFIRST],s2); if ((sym->usage & uNATIVE)!=0) {
break; /* reserve a SYSREQ id if called for the first time */
} /* case */ if (sc_status==statWRITE && (sym->usage & uREAD)==0 && sym->addr>=0)
} /* switch */ sym->addr=ntv_funcid++;
outval(sym->addr,FALSE);
} else {
/* normal function, write its name instead of the address
* so that the address will be resolved at assemble time
*/
stgwrite(".");
stgwrite(sym->name);
} /* if */
} else {
outval(sym->addr,FALSE);
} /* if */
/* mark symbol as "used", unknown whether for read or write */
markusage(sym,uREAD | uWRITTEN);
code_idx+=opargs(1);
} /* if */
break;
default: {
char s2[20];
extern char *sc_tokens[];/* forward declaration */
if (tok<256)
sprintf(s2,"%c",(char)tok);
else
strcpy(s2,sc_tokens[tok-tFIRST]);
error(1,sc_tokens[tSYMBOL-tFIRST],s2);
break;
} /* case */
} /* switch */
} /* if */
stgwrite("\n");
check_empty(lptr);
} /* if */ } /* if */
stgwrite("\n");
check_empty(lptr);
break; break;
} /* case */ } /* case */
#endif #endif