Changeset 2081 for rtcg

Show
Ignore:
Timestamp:
01/19/10 14:59:03 (2 years ago)
Author:
mwhitworth
Message:

Add other new files to SVN.

Location:
rtcg/trunk/src
Files:
9 added
6 modified

Legend:

Unmodified
Added
Removed
  • rtcg/trunk/src/asm_arith.c

    r2079 r2081  
    11#include "asm_arith.h" 
    22#include <stdio.h> 
    3  
    4 #define X86_PURE_REGISTER       3 
     3#include <stdlib.h> 
     4#include <x86/modrm.h> 
    55 
    66/* See the "Intel Architecture Software Developer’s Manual Volume 2: Instruction 
     
    1616        AsmModRm(modRm, &mod, &reg, &rm); 
    1717         
    18         printf("mod = %u, reg = %u, rm = %u\n", mod, reg, rm); 
    19          
    20         if (mod == X86_PURE_REGISTER) 
     18        if (mod == MOD_REG) 
    2119                return 2; /* instruction byte and ModRM byte. */ 
    2220         
    23         if (!mod) 
     21        if (mod == MOD_REG_MEM) 
    2422        { 
    2523                if (rm == 4) 
  • rtcg/trunk/src/asm_write.c

    r2079 r2081  
    22#include <stdlib.h> 
    33#include <string.h> 
     4#include <x86/modrm.h> 
    45 
    56#include "asm.h" 
     
    5556                                        offset+=5; 
    5657                                        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; 
    5764                                         
    5865                                case INSTR_NOP: 
     
    7380                                        exit(0); 
    7481                        } 
    75                          
    76 //                      printf("oInstr = %#X (%#X %#X)\n", oInstr, *(unsigned long*)oInstr, *(unsigned long*)(oInstr+4)); 
    7782                } 
    7883                 
    7984                curr=curr->next;         
    8085        } 
    81          
    82 //      printf("New code length has %d bytes\n", offset); 
    8386 
    8487        return offset; 
  • rtcg/trunk/src/block.c

    r2079 r2081  
    165165                                AsmCreateBranches(curr, trueStart, falseStart); 
    166166                                 
    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);                                
    168169 
    169170                                curr->hasBranch=1; 
  • rtcg/trunk/src/main.c

    r2079 r2081  
    7777        AsmGenerateContext(&context, DoTest); 
    7878 
    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; 
    8181 
    82         file.read = AsmGenerate(&context); 
     82        /*file.read =*/ AsmGenerate(&context); 
    8383 
    8484#if 0 
  • rtcg/trunk/src/newasm.c

    r2079 r2081  
    22#include <stdlib.h> 
    33#include <string.h> 
     4 
     5#include <debug.h> 
     6 
     7#include <x86/modrm.h> 
    48 
    59#include "asm.h" 
     
    8488                                case INSTR_SUB: 
    8589                                { 
    86                                         int modRm=instruction[1]; 
    87  
    8890                                        unsigned char mod, reg, rm; 
    8991                                         
    90                                         AsmModRm(modRm, &mod, &reg, &rm);                                
    91                                  
    92                                         if (modRm & 0x80) 
     92                                        AsmModRm(instruction[1], &mod, &reg, &rm);                               
     93 
     94                                        switch (mod) 
    9395                                        { 
    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; 
    105129                                        } 
    106130                                         
     
    171195                                         
    172196                                        ins->operandTypes=(INSTR_REG << DEST_SHIFT) | INSTR_IMM; 
    173                                          
     197                                 
     198                                        /* FIXME: May be wrong */ 
    174199                                        memcpy(&ins->imSrc, &instruction[1], 4); 
    175200                                        ins->regDest=ASM_REG_EAX; 
     
    196221                } 
    197222         
    198                 if (curr == curr->next) 
    199                         break; 
    200  
    201                 printf("curr = %#X, next = %#X\n", curr, curr->next); 
    202223                curr=curr->next; 
    203224        } 
    204225} 
    205226 
     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 
    206234void AsmClearDeadCode(struct AsmContext* context) 
    207235{ 
     236        TRACE_ENTER(); 
     237 
    208238        struct AsmBlock* curr=context->head; 
    209239         
     
    219249                        free(curr->prev); 
    220250        } 
     251 
     252        TRACE_EXIT(); 
    221253} 
    222254 
    223255void AsmAddConstantsBlock(struct AsmContext* context, struct AsmBlock* block, unsigned long* regValues[]) 
    224256{ 
     257        TRACE_ENTER(); 
     258 
    225259        int i; 
    226260        struct AsmInstruction* testIns=NULL; 
    227261         
    228         /* TODO: Better solution? */ 
    229262        unsigned long* stack[16]; 
    230263        int currP=0; 
     
    261294                                        { 
    262295                                                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]); 
    264297                                         
    265298                                                regValues[ins->regDest]=&context->params[param]; 
     
    341374         
    342375out: 
     376        TRACE_EXIT(); 
    343377        return; 
    344378} 
     
    346380void AsmAddConstants(struct AsmContext* context) 
    347381{ 
     382        TRACE_ENTER(); 
     383 
    348384        unsigned long* regValues[8]; 
    349385        int paramOffset=2; 
    350          
    351         regValues[ASM_REG_ESP]=&paramOffset; 
     386 
     387        regValues[ASM_REG_ESP]=(unsigned long*)&paramOffset; 
    352388         
    353389        AsmAddConstantsBlock(context, context->head, regValues); 
     390 
     391        TRACE_EXIT(); 
    354392} 
    355393 
     
    454492} 
    455493 
     494/* Main function */ 
    456495void* AsmGenerate(struct AsmContext* context) 
    457496{ 
     
    464503        /* Dead code elimination. Remove blocks with no references. */   
    465504        AsmClearDeadCode(context); 
    466          
    467505        AsmMergeBlocks(context); 
    468  
    469506        AsmRemoveUnused(context); 
    470507 
    471         length=AsmWriteCode(context, out); 
     508        length = AsmWriteCode(context, out); 
    472509 
    473510        printf("Function length: %u bytes.\n", length); 
     
    475512        AsmPrintCode(out, length); 
    476513         
    477         /* Check if we're at the end of the function. */ 
    478          
    479 //      free(out); 
    480          
    481514        return out; 
    482515} 
  • rtcg/trunk/src/print.c

    r2079 r2081  
    11#include "asm.h" 
    22 
    3 char* regs[]={"eax", "ecx", "edx", "ebx"}; 
     3#define _GNU_SOURCE 
     4#include <stdio.h> 
     5#include <stdlib.h> 
    46 
    5 void AsmPrintCode(char* out, int length) 
     7#include <x86/modrm.h> 
     8 
     9char* regs[]={"eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi"}; 
     10 
     11void AsmPrintCode(char* code, int length, FILE* outBuffer, int outLength) 
    612{ 
    713        int i=0; 
    8         unsigned char* ins=(unsigned char*)out; 
     14        unsigned char* ins=(unsigned char*)code; 
    915         
    10         printf("optimizedCode:\n"); 
    11          
    12         while (ins < out+length) 
     16        while (ins < (unsigned char*)code+length) 
    1317        { 
     18                printf("\t"); 
     19 
    1420                switch (*ins) 
    1521                { 
     
    2228                                unsigned char mod, reg, rm; 
    2329                                AsmModRm(ins[1], &mod, &reg, &rm); 
    24                                 printf("\txor\t%s, %s\n", regs[reg], regs[rm]); 
     30                                printf("xor\t%s, %s\n", regs[reg], regs[rm]); 
    2531                                ins+=2; 
    2632                                break; 
     
    2834                                 
    2935                        case INSTR_TEST: 
    30                                 printf("\ttest ecx, ecx\n"); 
     36                                printf("test ecx, ecx\n"); 
    3137                                ins+=2; 
    3238                                break; 
     
    3541                        { 
    3642                                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]); 
    3844                                ins+=5; 
    3945                                break; 
     
    4248                        case INSTR_PUSH_START ... INSTR_PUSH_STOP: 
    4349                        { 
    44                                 printf("\tpush\t%s\n", regs[*ins-INSTR_PUSH_START]); 
     50                                printf("push\t%s\n", regs[*ins-INSTR_PUSH_START]); 
    4551                                ins+=1; 
     52                                break; 
     53                        } 
     54 
     55                        case INSTR_SUB: 
     56                        { 
     57                                unsigned char mod, reg, rm; 
     58                                AsmModRm(ins[1], &mod, &reg, &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; 
    4672                                break; 
    4773                        } 
     
    4975                        case INSTR_POP_START ... INSTR_POP_STOP: 
    5076                        { 
    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]); 
    5378                                ins+=1; 
    5479                                break; 
     
    5681                                 
    5782                        case INSTR_RET: 
    58                                 printf("\tret\n"); 
     83                                printf("ret"); 
    5984                                ins++; 
    6085                                break; 
     
    6489                                ins++; 
    6590                } 
     91 
     92                printf("\n"); 
    6693        } 
    6794}