void createOutputFile(FILE* input, FILE* output, MacroTable* table ){ char* instruc = (char*)calloc(100,sizeof(char)); char* prev= (char*)calloc(100,sizeof(char)); char* parameter= (char*)calloc(100,sizeof(char)); char* oper = (char*)calloc(100,sizeof(char)); int index=-1; while(fscanf(input,"%s",instruc) > 0){ // If the operation with its operand is found, its mnemonic and operand is printed if(IsKeyword(instruc)==2){ fscanf(input,"%s",oper); strcat(instruc," "); strcat(instruc,oper); fprintf(output,"%s\n",instruc); } // If the operation without any operand is found, its mnemonic is printed if(IsKeyword(instruc)==1){ fprintf(output,"%s\n",instruc); } // finds a macro definition, and ignores its definition if(IsLabel(instruc)==1){ remove2P(instruc); if(isMacro(table,instruc)==1||isMacro(table,instruc)==2){//find a macro definition while(strcmp(instruc,"ENDMACRO")!=0){ fscanf(input,"%s",instruc);//ignores its definition } } else{ //if is label: strcat(instruc,":"); fprintf(output,"%s ",instruc); } } // prints macro whithout parameter in the output file if(isMacro(table,instruc)==1){ PrintMacroTableInFile(output,table, findLabelName(table,instruc)); } // prints macro with parameter if(isMacro(table,instruc)==2){ fscanf(input,"%s",parameter); PrintMacroTableInFileParameter(output,table, findLabelName(table,instruc),parameter); } } }
EXPR *FindBasic( EXPR *def ) { unsigned int i ; EXPR *expr, *E1, *E2 ; if( IsReal(def) ) { expr = new UNIT ; U(expr)->Ehead = String( "Ordinate" ) ; ListAppend( expr, def->Copy() ) ; return expr ; } if( IsLabel( def ) ) { for( i = 0 ; i < U(UnitList)->used ; i++ ) { if( U(UN(i))->Ehead == def ) { expr = UN(i)->Copy() ; delete U(expr)->Eeval ; U(expr)->Eeval = NULL_EXPR ; return expr ; } } IOerror( IO_ERR, "FindBasic", "%s is an undefined unit", LabelValue(def) ) ; return garbageunit() ; } if( !IsOper(def) ) { IOerror( IO_FATAL, "FindBasic", "unexpected in units definition" ) ; return garbageunit() ; } if( D(def)->oper == OPER_MINUS ) { expr = FindBasic( D(def)->Eleft ) ; if( U(expr)->used > 1 ) { IOerror( IO_ERR, "FindBasic", "illegal negation of unit" ) ; return garbageunit() ; } E1 = OP3( "u-", U(expr)->list[0], NULL_EXPR ) ; delete U(expr)->list[0] ; U(expr)->list[0] = E1 ; return expr ; } E1 = FindBasic( D(def)->Eleft ) ; E2 = FindBasic( D(def)->Eright ) ; switch( D(def)->oper ) { case OPER_MULTIPLY: expr = UnitOP3( "*", "+", E1, E2 ) ; break ; case OPER_DIVIDE: expr = UnitOP3( "/", "-", E1, E2 ) ; break ; case OPER_POW: expr = UnitOP3( "^", "*", E1, E2 ) ; break ; default: IOerror( IO_ERR, "FindBasic", "unexpected operator in unit" ) ; expr = garbageunit() ; break ; } delete E1 ; delete E2 ; return expr ; }
void DumpBytecode(char *filename, char *origname, char *stringsname) { int pushed = 0, i = 0; outfile = fopen(filename,"w"); fprintf(outfile,"// Disassembly of %s\n\n",origname); fprintf(outfile,"#include %s\n\n",stringsname); // parser stuff fseek(infile,scrhead.bytecode.offset,SEEK_SET); while(ftell(infile)<scrhead.bytecode.offset+scrhead.bytecode.size) { int o = 0, id = 0, id2 = 0; fprintf(outfile,"%08x ",ftell(infile)); id = IsLabel(labels,ftell(infile),scrhead.labels.size); if(id!=-1) fprintf(outfile,"\nSetLabel %d:\n",id); id2 = IsLabel(markers,ftell(infile),scrhead.markers.size); if(id2!=-1) { if(id==-1) fprintf(outfile,"\n"); fprintf(outfile,"SetMarker %d:\n",id2); } fread(&o,1,1,infile); if(o<0 || o>0x32) { printf("Invalid opcode %02x found at %08x\n",opcode,ftell(infile)-1); exit(1); } else { int var = 0; char var2 = 0; if(strcmp(opcode[o].name,"")==0 || opcode[o].type==0) fprintf(outfile,"[%02x]",o); else fprintf(outfile,"%s", opcode[o].name); fprintf(outfile,"("); if(opcode[o].type != 7) { switch(opcode[o].type) { case 0: fprintf(outfile,"%s",opcode[o].name); break; case 1: break; case 2: fread(&var,1,4,infile); fprintf(outfile,"%08x",var); break; case 3: fread(&var,1,4,infile); fprintf(outfile,"%08x, ",var); fread(&var,1,4,infile); fprintf(outfile,"%08x",var); break; case 4: fread(&var,1,4,infile); fprintf(outfile,"%08x, ",var); fread(&var,1,4,infile); fprintf(outfile,"%08x, ",var); fread(&var,1,4,infile); fprintf(outfile,"%08x",var); break; case 5: fread(&var,1,4,infile); fprintf(outfile,"%08x, ",var); fread(&var2,1,1,infile); fprintf(outfile,"%02x",var2); break; case 6: fread(&var,1,4,infile); fprintf(outfile,"%08x, ",var); fread(&var,1,4,infile); fprintf(outfile,"%08x, ",var); fread(&var2,1,1,infile); fprintf(outfile,"%02x",var2); break; } } else { switch(o) { case 0x02: { fread(&var,1,4,infile); fprintf(outfile,"%08x, ",var); switch(var) { case 0x0a: // imm fread(&var,1,4,infile); fprintf(outfile,"%08x",var); pushed = var; break; case 0x14: // string fread(&var,1,4,infile); fprintf(outfile,"#<%04d>",var); break; } } break; case 0x15: case 0x30: { int count = 0; //printf("%08x\n",ftell(infile)-1); if(o==0x30) { fread(&var,1,4,infile); fprintf(outfile,"%08x, ",var); } fread(&count,1,4,infile); fprintf(outfile,"%08x",count); while(--count>=0) { fread(&var,1,4,infile); fprintf(outfile,", %08x",var); } if(o==0x30) { fread(&count,1,4,infile); fprintf(outfile,", %08x",count); if(count>0) { //printf("%08x\n",count); exit(1); } if(count-->0) { fread(&var,1,4,infile); fprintf(outfile,", %08x",var); } fread(&var,1,4,infile); fprintf(outfile,", %08x",var); // pushed==0x04 || pushed==0x05 if(pushed==0x0c || pushed==0x4c || pushed==0x4d) { fread(&var,1,4,infile); fprintf(outfile,", %08x",var); } } } break; } } } fprintf(outfile,")\n"); } for(i=0; i<scrhead.unk12.size; i++) fprintf(outfile,"\n@%08x",unk12[i]); for(i=0; i<scrhead.unk13.size; i++) fprintf(outfile,"\n#%08x",unk13[i]); printf("Dumped bytecode to %s\n",filename); fclose(outfile); }