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

Add other new files to SVN.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • 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}