static void handle_equ(char *s) { char *labname; symbol *label; if(!(labname=parse_identifier(&s))){ syntax_error(10); /* identifier expected */ return; } s=skip(s); if(*s!=',') syntax_error(9); else s=skip(s+1); label=new_abs(labname,parse_expr_tmplab(&s)); myfree(labname); eol(s); }
int main(int argc,char **argv) { int i; for(i=1; i<argc; i++) { if(argv[i][0]=='-'&&argv[i][1]=='F') { output_format=argv[i]+2; argv[i][0]=0; } if(!strcmp("-quiet",argv[i])) { verbose=0; argv[i][0]=0; } } if(!init_output(output_format)) general_error(16,output_format); if(!init_main()) general_error(10,"main"); if(verbose) printf("%s\n%s\n%s\n%s\n",copyright,cpu_copyright,syntax_copyright,output_copyright); for(i=1; i<argc; i++) { if(argv[i][0]==0) continue; if(argv[i][0]!='-') { if(inname) general_error(11); inname=argv[i]; continue; } if(!strcmp("-o",argv[i])&&i<argc-1) { if(outname) general_error(28,'o'); outname=argv[++i]; continue; } if(!strcmp("-L",argv[i])&&i<argc-1) { if(listname) general_error(28,'L'); listname=argv[++i]; produce_listing=1; continue; } if(!strcmp("-Lnf",argv[i])) { listformfeed=0; continue; } if(!strcmp("-Lns",argv[i])) { listnosyms=1; continue; } if(!strncmp("-Ll",argv[i],3)) { sscanf(argv[i]+3,"%i",&listlinesperpage); continue; } if(!strncmp("-D",argv[i],2)) { char *def=NULL; expr *val; if(argv[i][2]) def=&argv[i][2]; else if (i<argc-1) def=argv[++i]; if(def) { char *s=def; if(ISIDSTART(*s)) { s++; while(ISIDCHAR(*s)) s++; def=cnvstr(def,s-def); if(*s=='=') { s++; val=parse_expr(&s); } else val=number_expr(1); if(*s) general_error(23,'D'); /* trailing garbage after option */ new_abs(def,val); myfree(def); continue; } } } if(!strncmp("-I",argv[i],2)) { char *path=NULL; if(argv[i][2]) path=&argv[i][2]; else if (i<argc-1) path=argv[++i]; if(path) { new_include_path(path); continue; } } if(!strcmp("-unnamed-sections",argv[i])) { unnamed_sections=1; continue; } if(!strcmp("-ignore-mult-inc",argv[i])) { ignore_multinc=1; continue; } if(!strcmp("-nocase",argv[i])) { nocase=1; continue; } if(!strcmp("-noesc",argv[i])) { esc_sequences=0; continue; } if(!strcmp("-nosym",argv[i])) { no_symbols=1; continue; } if(!strncmp("-nowarn=",argv[i],8)) { int wno; sscanf(argv[i]+8,"%i",&wno); disable_warning(wno); continue; } else if(!strcmp("-w",argv[i])) { no_warn=1; continue; } if(!strncmp("-maxerrors=",argv[i],11)) { sscanf(argv[i]+11,"%i",&max_errors); continue; } else if(!strcmp("-pic",argv[i])) { pic_check=1; continue; } if(cpu_args(argv[i])) continue; if(syntax_args(argv[i])) continue; if(output_args(argv[i])) continue; if (!strncmp("-x",argv[i],2)) { auto_import=0; continue; } general_error(14,argv[i]); } if(inname) { setfilename(inname); setdebugname(inname); include_source(inname); } else general_error(15); if(!init_parse()) general_error(10,"parse"); if(!init_syntax()) general_error(10,"syntax"); if(!init_cpu()) general_error(10,"cpu"); parse(); if(errors==0||produce_listing) resolve(); if(errors==0||produce_listing) assemble(); if(errors==0&&!auto_import) undef_syms(); if(!listname) listname="a.lst"; if(produce_listing) write_listing(listname); if(!outname) outname="a.out"; if(errors==0) { if(verbose) statistics(); outfile=fopen(outname,"wb"); if(!outfile) general_error(13,outname); write_object(outfile,first_section,first_symbol); } leave(); return 0; /* not reached */ }
/* Very simple, very incomplete. */ void parse(void) { char *s,*line,*inst,*ext[MAX_QUALIFIERS?MAX_QUALIFIERS:1],*op[MAX_OPERANDS]; int inst_len,ext_len[MAX_QUALIFIERS?MAX_QUALIFIERS:1],op_len[MAX_OPERANDS]; int i,ext_cnt,op_cnt,par_cnt; instruction *ip; while(line=read_next_line()){ s=line; if(isalnum((unsigned char)*s)){ /* Handle labels at beginning of line */ char *labname,*equ; symbol *label; while(*s&&!isspace((unsigned char)*s)&&*s!=':') s++; labname=cnvstr(line,s-line); s=skip(s+1); if(!strncmp(s,"equ",3)&&isspace((unsigned char)s[3])){ s=skip(s+3); label=new_abs(labname,parse_expr(&s)); }else{ label=new_labsym(0,labname); add_atom(0,new_label_atom(label)); } free(labname); } s=parse_cpu_special(s); if(handle_directive(s)) continue; /* skip spaces */ s=skip(s); if(!*s) continue; /* read mnemonic name */ inst=s; if(!ISIDSTART(*s)){ syntax_error(10); continue; } #if MAX_QUALIFIERS==0 while(*s&&!isspace((unsigned char)*s)) s++; #else while(*s&&*s!='.'&&!isspace((unsigned char)*s)) s++; #endif inst_len=s-inst; /* read qualifiers */ ext_cnt=0; while(*s=='.'&&ext_cnt<MAX_QUALIFIERS){ s++; ext[ext_cnt]=s; while(*s&&*s!='.'&&!isspace((unsigned char)*s)) s++; ext_len[ext_cnt]=s-ext[ext_cnt]; if(ext_len[ext_cnt]<=0) syntax_error(1); else ext_cnt++; } if(!isspace((unsigned char)*s)) syntax_error(2); /* read operands, terminated by comma (unless in parentheses) */ s=skip(s); op_cnt=0; while(*s&&op_cnt<MAX_OPERANDS){ op[op_cnt]=s; s=skip_operand(s); op_len[op_cnt]=s-op[op_cnt]; if(op_len[op_cnt]<=0) syntax_error(5); else op_cnt++; s=skip(s); if(*s!=','){ break; }else{ s=skip(s+1); } } s=skip(s); if(*s!=0) syntax_error(6); ip=new_inst(inst,inst_len,op_cnt,op,op_len); #if MAX_QUALIFIERS>0 if(ip){ for(i=0;i<ext_cnt;i++) ip->qualifiers[i]=cnvstr(ext[i],ext_len[i]); for(;i<MAX_QUALIFIERS;i++) ip->qualifiers[i]=0; } #endif if(ip){ add_atom(0,new_inst_atom(ip)); }else ; } }
static void handle_set(char *s) { new_abs(labname,parse_expr_tmplab(&s)); eol(s); }