- Timestamp:
- 01/19/10 14:59:03 (2 years ago)
- Location:
- rtcg/trunk/src
- Files:
-
- 9 added
- 6 modified
-
Makefile (added)
-
TODO (added)
-
asm.h (added)
-
asm_arith.c (modified) (2 diffs)
-
asm_arith.h (added)
-
asm_ins.h (added)
-
asm_write.c (modified) (3 diffs)
-
block.c (modified) (1 diff)
-
inc/debug.h (added)
-
main.c (modified) (1 diff)
-
newasm.c (modified) (11 diffs)
-
print.c (modified) (8 diffs)
-
tests (added)
-
x86 (added)
-
x86/modrm.h (added)
Legend:
- Unmodified
- Added
- Removed
-
rtcg/trunk/src/asm_arith.c
r2079 r2081 1 1 #include "asm_arith.h" 2 2 #include <stdio.h> 3 4 # define X86_PURE_REGISTER 33 #include <stdlib.h> 4 #include <x86/modrm.h> 5 5 6 6 /* See the "Intel Architecture Software Developerâs Manual Volume 2: Instruction … … 16 16 AsmModRm(modRm, &mod, ®, &rm); 17 17 18 printf("mod = %u, reg = %u, rm = %u\n", mod, reg, rm); 19 20 if (mod == X86_PURE_REGISTER) 18 if (mod == MOD_REG) 21 19 return 2; /* instruction byte and ModRM byte. */ 22 20 23 if ( !mod)21 if (mod == MOD_REG_MEM) 24 22 { 25 23 if (rm == 4) -
rtcg/trunk/src/asm_write.c
r2079 r2081 2 2 #include <stdlib.h> 3 3 #include <string.h> 4 #include <x86/modrm.h> 4 5 5 6 #include "asm.h" … … 55 56 offset+=5; 56 57 break; 58 59 case INSTR_SUB: 60 oInstr[1] = AsmMakeModRm(MOD_REG, instr->regDest, instr->regSrc); 61 oInstr[3] = instr->imSrc; 62 offset += 4; 63 break; 57 64 58 65 case INSTR_NOP: … … 73 80 exit(0); 74 81 } 75 76 // printf("oInstr = %#X (%#X %#X)\n", oInstr, *(unsigned long*)oInstr, *(unsigned long*)(oInstr+4));77 82 } 78 83 79 84 curr=curr->next; 80 85 } 81 82 // printf("New code length has %d bytes\n", offset);83 86 84 87 return offset; -
rtcg/trunk/src/block.c
r2079 r2081 165 165 AsmCreateBranches(curr, trueStart, falseStart); 166 166 167 printf("true = %#X, false = %#X\n", curr->trueBranch->start, curr->falseBranch->start); 167 printf("true = %p, false = %p\n", 168 (void*)curr->trueBranch->start, (void*)curr->falseBranch->start); 168 169 169 170 curr->hasBranch=1; -
rtcg/trunk/src/main.c
r2079 r2081 77 77 AsmGenerateContext(&context, DoTest); 78 78 79 context.params[0] = &file;80 context.invariants[0] = file.vNode;79 context.params[0] = (unsigned long)&file; 80 context.invariants[0] = (unsigned long)file.vNode; 81 81 82 file.read =AsmGenerate(&context);82 /*file.read =*/ AsmGenerate(&context); 83 83 84 84 #if 0 -
rtcg/trunk/src/newasm.c
r2079 r2081 2 2 #include <stdlib.h> 3 3 #include <string.h> 4 5 #include <debug.h> 6 7 #include <x86/modrm.h> 4 8 5 9 #include "asm.h" … … 84 88 case INSTR_SUB: 85 89 { 86 int modRm=instruction[1];87 88 90 unsigned char mod, reg, rm; 89 91 90 AsmModRm( modRm, &mod, ®, &rm);91 92 if (modRm & 0x80)92 AsmModRm(instruction[1], &mod, ®, &rm); 93 94 switch (mod) 93 95 { 94 instruction+=3; 95 96 ins->operandTypes=(INSTR_REG << DEST_SHIFT) | INSTR_IMM; 97 ins->regDest = reg; 98 ins->imSrc = 0; 99 }else{ 100 instruction+=5; 101 ins->operandTypes=(INSTR_MEMR << DEST_SHIFT) | INSTR_IMM; 102 ins->regDest=ASM_REG_ESP; 103 ins->disp=8; 104 ins->imSrc=0; 96 case MOD_REG_MEM: 97 ins->operandTypes = (INSTR_REG << DEST_SHIFT) | 98 INSTR_MEMR; 99 100 ins->regDest = reg; 101 ins->imSrc = (int)instruction[3]; 102 103 instruction += AsmModRmBytes(instruction[1]); 104 instruction++; /* FIX */ 105 106 break; 107 108 case MOD_REG_MEM_BYTE: 109 break; 110 111 case MOD_REG_MEM_WORD: 112 break; 113 114 case MOD_REG: 115 instruction += 3; 116 ins->operandTypes=(INSTR_REG << DEST_SHIFT) | INSTR_IMM; 117 ins->regDest = reg; 118 119 /* FIXME: Check for different sizes of operand */ 120 ins->imSrc = (int)instruction[2]; 121 break; 122 123 default: 124 instruction+=5; 125 ins->operandTypes=(INSTR_MEMR << DEST_SHIFT) | INSTR_IMM; 126 ins->regDest=ASM_REG_ESP; 127 ins->disp=8; 128 ins->imSrc=0; 105 129 } 106 130 … … 171 195 172 196 ins->operandTypes=(INSTR_REG << DEST_SHIFT) | INSTR_IMM; 173 197 198 /* FIXME: May be wrong */ 174 199 memcpy(&ins->imSrc, &instruction[1], 4); 175 200 ins->regDest=ASM_REG_EAX; … … 196 221 } 197 222 198 if (curr == curr->next)199 break;200 201 printf("curr = %#X, next = %#X\n", curr, curr->next);202 223 curr=curr->next; 203 224 } 204 225 } 205 226 227 /* 228 * AsmClearDeadCode 229 * 230 * If there are no references to a block, delete it. This may happen 231 * because an if test now returns a constant value 232 */ 233 206 234 void AsmClearDeadCode(struct AsmContext* context) 207 235 { 236 TRACE_ENTER(); 237 208 238 struct AsmBlock* curr=context->head; 209 239 … … 219 249 free(curr->prev); 220 250 } 251 252 TRACE_EXIT(); 221 253 } 222 254 223 255 void AsmAddConstantsBlock(struct AsmContext* context, struct AsmBlock* block, unsigned long* regValues[]) 224 256 { 257 TRACE_ENTER(); 258 225 259 int i; 226 260 struct AsmInstruction* testIns=NULL; 227 261 228 /* TODO: Better solution? */229 262 unsigned long* stack[16]; 230 263 int currP=0; … … 261 294 { 262 295 int param=(ins->disp >> *regValues[ASM_REG_ESP])-1; 263 printf("disp = %d, param %d, % d\n", ins->disp, param, *regValues[ASM_REG_ESP]);296 printf("disp = %d, param %d, %lu\n", ins->disp, param, *regValues[ASM_REG_ESP]); 264 297 265 298 regValues[ins->regDest]=&context->params[param]; … … 341 374 342 375 out: 376 TRACE_EXIT(); 343 377 return; 344 378 } … … 346 380 void AsmAddConstants(struct AsmContext* context) 347 381 { 382 TRACE_ENTER(); 383 348 384 unsigned long* regValues[8]; 349 385 int paramOffset=2; 350 351 regValues[ASM_REG_ESP]= ¶mOffset;386 387 regValues[ASM_REG_ESP]=(unsigned long*)¶mOffset; 352 388 353 389 AsmAddConstantsBlock(context, context->head, regValues); 390 391 TRACE_EXIT(); 354 392 } 355 393 … … 454 492 } 455 493 494 /* Main function */ 456 495 void* AsmGenerate(struct AsmContext* context) 457 496 { … … 464 503 /* Dead code elimination. Remove blocks with no references. */ 465 504 AsmClearDeadCode(context); 466 467 505 AsmMergeBlocks(context); 468 469 506 AsmRemoveUnused(context); 470 507 471 length =AsmWriteCode(context, out);508 length = AsmWriteCode(context, out); 472 509 473 510 printf("Function length: %u bytes.\n", length); … … 475 512 AsmPrintCode(out, length); 476 513 477 /* Check if we're at the end of the function. */478 479 // free(out);480 481 514 return out; 482 515 } -
rtcg/trunk/src/print.c
r2079 r2081 1 1 #include "asm.h" 2 2 3 char* regs[]={"eax", "ecx", "edx", "ebx"}; 3 #define _GNU_SOURCE 4 #include <stdio.h> 5 #include <stdlib.h> 4 6 5 void AsmPrintCode(char* out, int length) 7 #include <x86/modrm.h> 8 9 char* regs[]={"eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi"}; 10 11 void AsmPrintCode(char* code, int length, FILE* outBuffer, int outLength) 6 12 { 7 13 int i=0; 8 unsigned char* ins=(unsigned char*) out;14 unsigned char* ins=(unsigned char*)code; 9 15 10 printf("optimizedCode:\n"); 11 12 while (ins < out+length) 16 while (ins < (unsigned char*)code+length) 13 17 { 18 printf("\t"); 19 14 20 switch (*ins) 15 21 { … … 22 28 unsigned char mod, reg, rm; 23 29 AsmModRm(ins[1], &mod, ®, &rm); 24 printf(" \txor\t%s, %s\n", regs[reg], regs[rm]);30 printf("xor\t%s, %s\n", regs[reg], regs[rm]); 25 31 ins+=2; 26 32 break; … … 28 34 29 35 case INSTR_TEST: 30 printf(" \ttest ecx, ecx\n");36 printf("test ecx, ecx\n"); 31 37 ins+=2; 32 38 break; … … 35 41 { 36 42 char regs[]="acdb"; 37 printf(" \tmov\te%cx, %#x\n", regs[*ins-0xB8], *(unsigned long*)&ins[1]);43 printf("mov\te%cx, %#lx\n", regs[*ins-0xB8], *(unsigned long*)&ins[1]); 38 44 ins+=5; 39 45 break; … … 42 48 case INSTR_PUSH_START ... INSTR_PUSH_STOP: 43 49 { 44 printf(" \tpush\t%s\n", regs[*ins-INSTR_PUSH_START]);50 printf("push\t%s\n", regs[*ins-INSTR_PUSH_START]); 45 51 ins+=1; 52 break; 53 } 54 55 case INSTR_SUB: 56 { 57 unsigned char mod, reg, rm; 58 AsmModRm(ins[1], &mod, ®, &rm); 59 60 switch (mod) 61 { 62 case MOD_REG: 63 printf("sub [%s], %#X", regs[rm], ins[3]); 64 break; 65 66 default: 67 printf("INSTR_SUB: TODO\n"); 68 exit(0); 69 } 70 71 ins += 4; 46 72 break; 47 73 } … … 49 75 case INSTR_POP_START ... INSTR_POP_STOP: 50 76 { 51 char* regs[]={"eax", "ecx", "edx", "ebx"}; 52 printf("\tpop\t%s\n", regs[*ins-INSTR_POP_START]); 77 printf("pop\t%s", regs[*ins-INSTR_POP_START]); 53 78 ins+=1; 54 79 break; … … 56 81 57 82 case INSTR_RET: 58 printf(" \tret\n");83 printf("ret"); 59 84 ins++; 60 85 break; … … 64 89 ins++; 65 90 } 91 92 printf("\n"); 66 93 } 67 94 }
