void relocateAllSymbols( void ) { int i; char* ptr; int offset; char* tmp; for(i = 0;i < llmne.instr_len;i++) { offset = 0; ptr = strtok(strdup(llmne.instr[i].ctx.line), " \t"); ptr = strtok(NULL, " \t"); if(!ptr) continue; if(strstr(ptr, "$$")) continue; if((tmp = strchr(ptr,'+'))) { offset = atoi( tmp + 1 ); tmp[0] = 0; } if(searchSymbols(ptr)) llmne.instr[i].opcode = llmne.instr[i].instr_code * 100 + searchSymbols(ptr)->offset + offset; } }
void relocateAllSymbols( void ) { int i; char* ptr; int offset; char* tmp; for(i = 0;i < hexmne.instr_len;i++) { offset = 0; ptr = strtok(strdup(hexmne.instr[i].ctx.line), " \t"); ptr = strtok(NULL, " \t"); if(!ptr) continue; if(strstr(ptr, "$$")) continue; if((tmp = strchr(ptr,'+'))) { offset = strtol( tmp + 1, NULL, 16 ); tmp[0] = 0; } if(searchSymbols(ptr)) hexmne.instr[i].opcode = (hexmne.instr[i].instr_code << 8) | (searchSymbols(ptr)->offset + offset); } }
void varprintf( char* buf ) { int i,j; int len; char* tmp; extern char* i_file; if( !buf ) return; len = strlen( buf ); for(i = 0;i < len;i++) { if( buf[i] == '$' ) { if ( strchr( buf + i, ' ' ) ) j = (strchr( buf + i, ' ')) - ( buf + i ); else j = len - i; tmp = malloc( j ); memset( tmp, 0, j ); memcpy( tmp, buf + i + 1, j - 1 ); if(!strcmp( tmp, "LINE" )) printf("%d", nline ); else if (!strcmp(tmp, "IFILE")) printf("%s", i_file ); else { if(searchSymbols(tmp)) printf("%d", searchSymbols(tmp)->offset ); else putchar('$'); } i += strlen( tmp ); } else putchar( buf[i] ); } }
struct llmne_instr InstrParse(TokenCtx* ctx) { /* TODO adds STORE,PRINT,SET and fix the multiplce action instruction and the address calculation */ int i, offset = 0; char* ptr = NULL; if(searchSymbols("$$")) (searchSymbols("$$"))->offset = nline - 1; int size = sizeof(instruction_set) / sizeof(struct lxs_mne); if( !strcmp( ctx->instr, "DISPLAY" ) ) { if(!strcmp(ctx->args[0],"INT")) offset = 0; else if(!strcmp(ctx->args[0],"HEX")) offset = 1; else if(!strcmp(ctx->args[0],"BIN")) offset = 2; else if(!strcmp(ctx->args[0],"CHAR")) offset = 3; else if(!strcmp(ctx->args[0],"STRING")) offset = 4; else offset = 0; return newInstr(ctx, 34, offset); } for(i = 0;i < size;i++) { if( !strcmp( ctx->instr, instruction_set[i].mne ) ) { if( instruction_set[i].opcode ) { if((ptr = strchr(ctx->args[0],'+'))) { offset = atoi( ptr + 1); ptr[0] = '\0'; } if(searchSymbols(ctx->args[0])) return newInstr(ctx, instruction_set[i].instr, (searchSymbols(ctx->args[0]))->offset + offset); else return newInstr(ctx, instruction_set[i].instr , atoi(ctx->args[0])); } else return newInstr(ctx, instruction_set[i].instr , instruction_set[i].instr); } } ptr = strdup(ctx->instr); int argc = ctx->argc; char** args = malloc( argc * sizeof(char**) ); for(i = 0;i < argc;i++) args[i] = trim(ctx->args[i]); return (struct llmne_instr) { "VAR" ,{ "VAR", ptr , args , argc }, 0, 0, atoi(ptr) };
struct llmne_instr InstrParse(TokenCtx* ctx) { /* TODO adds STORE,PRINT,SET and fix the multiplce action instruction and the address calculation */ int offset = 0; char* ptr = NULL; if(searchSymbols("$$")) (searchSymbols("$$"))->offset = nline - 1; if(!strcmp(ctx->instr,"READ")) { if((ptr = strchr(ctx->args[0],'+'))) { offset = atoi(ptr+1); ptr[0] = '\0'; } if(searchSymbols(ctx->args[0])) return newInstr(ctx,10,(searchSymbols(ctx->args[0]))->offset + offset); else return newInstr(ctx,10,atoi(ctx->args[0])); } else if (!strcmp(ctx->instr,"WRITE")) { if((ptr = strchr(ctx->args[0],'+'))) { offset = atoi(ptr+1); ptr[0] = '\0'; } if(searchSymbols(ctx->args[0])) return newInstr(ctx,11,(searchSymbols(ctx->args[0]))->offset + offset); else return newInstr(ctx,11,atoi(ctx->args[0])); } else if (!strcmp(ctx->instr,"POP")) { if((ptr = strchr(ctx->args[0],'+'))) { offset = atoi(ptr+1); ptr[0] = '\0'; } if(searchSymbols(ctx->args[0])) return newInstr(ctx,12,(searchSymbols(ctx->args[0]))->offset + offset); else return newInstr(ctx,12,atoi(ctx->args[0])); } else if (!strcmp(ctx->instr,"PUSH")) { if((ptr = strchr(ctx->args[0],'+'))) { offset = atoi(ptr+1); ptr[0] = '\0'; } if(searchSymbols(ctx->args[0])) return newInstr(ctx,13,(searchSymbols(ctx->args[0]))->offset + offset); else return newInstr(ctx,13,atoi(ctx->args[0])); } else if (!strcmp(ctx->instr,"ADD")) { if((ptr = strchr(ctx->args[0],'+'))) { offset = atoi(ptr+1); ptr[0] = '\0'; } if(searchSymbols(ctx->args[0])) return newInstr(ctx,14,(searchSymbols(ctx->args[0]))->offset + offset); else return newInstr(ctx,14,atoi(ctx->args[0])); } else if (!strcmp(ctx->instr,"SUB")) { if((ptr = strchr(ctx->args[0],'+'))) { offset = atoi(ptr+1); ptr[0] = '\0'; } if(searchSymbols(ctx->args[0])) return newInstr(ctx,15,(searchSymbols(ctx->args[0]))->offset + offset); else return newInstr(ctx,15,atoi(ctx->args[0])); } else if (!strcmp(ctx->instr,"MUL")) { if((ptr = strchr(ctx->args[0],'+'))) { offset = atoi(ptr+1); ptr[0] = '\0'; } if(searchSymbols(ctx->args[0])) return newInstr(ctx,16,(searchSymbols(ctx->args[0]))->offset + offset); else return newInstr(ctx,16,atoi(ctx->args[0])); } else if (!strcmp(ctx->instr,"DIV")) { if((ptr = strchr(ctx->args[0],'+'))) { offset = atoi(ptr+1); ptr[0] = '\0'; } if(searchSymbols(ctx->args[0])) return newInstr(ctx,17,(searchSymbols(ctx->args[0]))->offset + offset); else return newInstr(ctx,17,atoi(ctx->args[0])); } else if (!strcmp(ctx->instr,"MOD")) { if((ptr = strchr(ctx->args[0],'+'))) { offset = atoi(ptr+1); ptr[0] = '\0'; } if(searchSymbols(ctx->args[0])) return newInstr(ctx,18,(searchSymbols(ctx->args[0]))->offset + offset); else return newInstr(ctx,18,atoi(ctx->args[0])); } else if (!strcmp(ctx->instr,"AND")) { if((ptr = strchr(ctx->args[0],'+'))) { offset = atoi(ptr+1); ptr[0] = '\0'; } if(searchSymbols(ctx->args[0])) return newInstr(ctx,19,(searchSymbols(ctx->args[0]))->offset + offset); else return newInstr(ctx,19,atoi(ctx->args[0])); } else if (!strcmp(ctx->instr,"OR")) { if((ptr = strchr(ctx->args[0],'+'))) { offset = atoi(ptr+1); ptr[0] = '\0'; } if(searchSymbols(ctx->args[0])) return newInstr(ctx,20,(searchSymbols(ctx->args[0]))->offset + offset); else return newInstr(ctx,20,atoi(ctx->args[0])); } else if (!strcmp(ctx->instr,"XOR")) { if((ptr = strchr(ctx->args[0],'+'))) { offset = atoi(ptr+1); ptr[0] = '\0'; } if(searchSymbols(ctx->args[0])) return newInstr(ctx,21,(searchSymbols(ctx->args[0]))->offset + offset); else return newInstr(ctx,21,atoi(ctx->args[0])); } else if (!strcmp(ctx->instr,"NOT")) { if((ptr = strchr(ctx->args[0],'+'))) { offset = atoi(ptr+1); ptr[0] = '\0'; } if(searchSymbols(ctx->args[0])) return newInstr(ctx,22,(searchSymbols(ctx->args[0]))->offset + offset); else return newInstr(ctx,22,atoi(ctx->args[0])); } else if (!strcmp(ctx->instr,"SHL")) { if((ptr = strchr(ctx->args[0],'+'))) { offset = atoi(ptr+1); ptr[0] = '\0'; } if(searchSymbols(ctx->args[0])) return newInstr(ctx,23,(searchSymbols(ctx->args[0]))->offset + offset); else return newInstr(ctx,23,atoi(ctx->args[0])); } else if (!strcmp(ctx->instr,"SHR")) { if((ptr = strchr(ctx->args[0],'+'))) { offset = atoi(ptr+1); ptr[0] = '\0'; } if(searchSymbols(ctx->args[0])) return newInstr(ctx,24,(searchSymbols(ctx->args[0]))->offset + offset); else return newInstr(ctx,24,atoi(ctx->args[0])); } else if (!strcmp(ctx->instr,"DEL")) { if((ptr = strchr(ctx->args[0],'+'))) { offset = atoi(ptr+1); ptr[0] = '\0'; } if(searchSymbols(ctx->args[0])) return newInstr(ctx,25,(searchSymbols(ctx->args[0]))->offset + offset); else return newInstr(ctx,25,atoi(ctx->args[0])); } else if (!strcmp(ctx->instr,"NOP")) { return newInstr(ctx,26,26); } else if (!strcmp(ctx->instr,"JMP")) { if((ptr = strchr(ctx->args[0],'+'))) { offset = atoi(ptr+1); ptr[0] = '\0'; } if(searchSymbols(ctx->args[0])) return newInstr(ctx,27,(searchSymbols(ctx->args[0]))->offset + offset); else return newInstr(ctx,27,atoi(ctx->args[0])); } else if (!strcmp(ctx->instr,"CMP")) { if((ptr = strchr(ctx->args[0],'+'))) { offset = atoi(ptr+1); ptr[0] = '\0'; } if(searchSymbols(ctx->args[0])) return newInstr(ctx,28,(searchSymbols(ctx->args[0]))->offset + offset); else return newInstr(ctx,28,atoi(ctx->args[0])); } else if (!strcmp(ctx->instr,"JN")) { if((ptr = strchr(ctx->args[0],'+'))) { offset = atoi(ptr+1); ptr[0] = '\0'; } if(searchSymbols(ctx->args[0])) return newInstr(ctx,29,(searchSymbols(ctx->args[0]))->offset + offset); else return newInstr(ctx,29,atoi(ctx->args[0])); } else if (!strcmp(ctx->instr,"JZ")) { if((ptr = strchr(ctx->args[0],'+'))) { offset = atoi(ptr+1); ptr[0] = '\0'; } if(searchSymbols(ctx->args[0])) return newInstr(ctx,30,(searchSymbols(ctx->args[0]))->offset + offset); else return newInstr(ctx,30,atoi(ctx->args[0])); } else if (!strcmp(ctx->instr,"JM")) { if((ptr = strchr(ctx->args[0],'+'))) { offset = atoi(ptr+1); ptr[0] = '\0'; } if(searchSymbols(ctx->args[0])) return newInstr(ctx,31,(searchSymbols(ctx->args[0]))->offset + offset); else return newInstr(ctx,31,atoi(ctx->args[0])); } else if (!strcmp(ctx->instr,"JG")) { if((ptr = strchr(ctx->args[0],'+'))) { offset = atoi(ptr+1); ptr[0] = '\0'; } if(searchSymbols(ctx->args[0])) return newInstr(ctx,32,(searchSymbols(ctx->args[0]))->offset + offset); else return newInstr(ctx,32,atoi(ctx->args[0])); } else if (!strcmp(ctx->instr,"EXIT")) { if((ptr = strchr(ctx->args[0],'+'))) { offset = atoi(ptr+1); ptr[0] = '\0'; } if(searchSymbols(ctx->args[0])) return newInstr(ctx,33,(searchSymbols(ctx->args[0]))->offset + offset); else return newInstr(ctx,33,atoi(ctx->args[0])); } else if (!strcmp(ctx->instr,"DISPLAY")) { if(!strcmp(ctx->args[0],"INT")) offset = 0; else if(!strcmp(ctx->args[0],"HEX")) offset = 1; else if(!strcmp(ctx->args[0],"BIN")) offset = 2; else if(!strcmp(ctx->args[0],"CHAR")) offset = 3; else if(!strcmp(ctx->args[0],"STRING")) offset = 4; else offset = 0; return newInstr(ctx,34,offset); } else if (!strcmp(ctx->instr,"INC")) { if((ptr = strchr(ctx->args[0],'+'))) { offset = atoi(ptr+1); ptr[0] = '\0'; } if(searchSymbols(ctx->args[0])) return newInstr(ctx,35,(searchSymbols(ctx->args[0]))->offset + offset); else return newInstr(ctx,35,atoi(ctx->args[0])); } else if (!strcmp(ctx->instr,"DEC")) { if((ptr = strchr(ctx->args[0],'+'))) { offset = atoi(ptr+1); ptr[0] = '\0'; } if(searchSymbols(ctx->args[0])) return newInstr(ctx,36,(searchSymbols(ctx->args[0]))->offset + offset); else return newInstr(ctx,36,atoi(ctx->args[0])); } else if (!strcmp(ctx->instr,"CALL")) { if((ptr = strchr(ctx->args[0],'+'))) { offset = atoi(ptr+1); ptr[0] = '\0'; } if(searchSymbols(ctx->args[0])) return newInstr(ctx,37,(searchSymbols(ctx->args[0]))->offset + offset); else return newInstr(ctx,37,atoi(ctx->args[0])); } else if (!strcmp(ctx->instr,"RET")) { return newInstr(ctx,38,00); } else if (!strcmp(ctx->instr,"STPUSH")) { if((ptr = strchr(ctx->args[0],'+'))) { offset = atoi(ptr+1); ptr[0] = '\0'; } if(searchSymbols(ctx->args[0])) return newInstr(ctx,39,(searchSymbols(ctx->args[0]))->offset + offset); else return newInstr(ctx,39,atoi(ctx->args[0])); } else if (!strcmp(ctx->instr,"STPOP")) { if((ptr = strchr(ctx->args[0],'+'))) { offset = atoi(ptr+1); ptr[0] = '\0'; } if(searchSymbols(ctx->args[0])) return newInstr(ctx,40,(searchSymbols(ctx->args[0]))->offset + offset); else return newInstr(ctx,40,atoi(ctx->args[0])); } else if (!strcmp(ctx->instr,"ADDSP")) { return newInstr(ctx,41,atoi(ctx->args[0])); } else if (!strcmp(ctx->instr,"SUBSP")) { return newInstr(ctx,42,atoi(ctx->args[0])); } ptr = strdup(ctx->instr); int i; int argc = ctx->argc; char** args = malloc( argc * sizeof(char**) ); for(i = 0;i < argc;i++) args[i] = trim(ctx->args[i]); return (struct llmne_instr) { "VAR" ,{ "VAR", ptr , args , argc }, 0, 0, atoi(ptr) };