|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <unistd.h> |
| 4 | +#include <string.h> |
| 5 | +#include <errno.h> |
| 6 | + |
| 7 | +#include "avs.h" |
| 8 | +#include "avs_x86.h" |
| 9 | + |
| 10 | +/* XXX: Duplicate / Reference */ |
| 11 | +static AvsNumber * get_operand(ILRegister *reg) |
| 12 | +{ |
| 13 | + switch (reg->type) { |
| 14 | + case ILRegisterTypeConstant: |
| 15 | + return ®->value.constant; |
| 16 | + |
| 17 | + case ILRegisterTypeVariable: |
| 18 | + return reg->value.variable->value; |
| 19 | + } |
| 20 | + |
| 21 | + return NULL; |
| 22 | +} |
| 23 | + |
| 24 | +static int compile_opcode(X86GlobalData *gd, ILInstruction *insn) |
| 25 | +{ |
| 26 | + switch (insn->type) { |
| 27 | + case ILInstructionAssign: |
| 28 | + x86_emit1(&gd->ctx, flds, offset(get_operand(insn->reg[2]))); |
| 29 | + x86_emit1(&gd->ctx, fsts, offset(get_operand(insn->reg[1]))); |
| 30 | + x86_emit1(&gd->ctx, fstps, offset(get_operand(insn->reg[0]))); |
| 31 | + break; |
| 32 | + |
| 33 | + case ILInstructionAdd: |
| 34 | + x86_emit1(&gd->ctx, flds, offset(get_operand(insn->reg[1]))); |
| 35 | + x86_emit1(&gd->ctx, fadds, offset(get_operand(insn->reg[2]))); |
| 36 | + x86_emit1(&gd->ctx, fstps, offset(get_operand(insn->reg[0]))); |
| 37 | + break; |
| 38 | + |
| 39 | + case ILInstructionSub: |
| 40 | + x86_emit1(&gd->ctx, flds, offset(get_operand(insn->reg[1]))); |
| 41 | + x86_emit1(&gd->ctx, fsubs, offset(get_operand(insn->reg[2]))); |
| 42 | + x86_emit1(&gd->ctx, fstps, offset(get_operand(insn->reg[0]))); |
| 43 | + break; |
| 44 | + |
| 45 | + case ILInstructionMul: |
| 46 | + x86_emit1(&gd->ctx, flds, offset(get_operand(insn->reg[1]))); |
| 47 | + x86_emit1(&gd->ctx, fmuls, offset(get_operand(insn->reg[2]))); |
| 48 | + x86_emit1(&gd->ctx, fstps, offset(get_operand(insn->reg[0]))); |
| 49 | + break; |
| 50 | + |
| 51 | + case ILInstructionDiv: |
| 52 | + x86_emit1(&gd->ctx, flds, offset(get_operand(insn->reg[1]))); |
| 53 | + x86_emit1(&gd->ctx, fdivs, offset(get_operand(insn->reg[2]))); |
| 54 | + x86_emit1(&gd->ctx, fstps, offset(get_operand(insn->reg[0]))); |
| 55 | + break; |
| 56 | + |
| 57 | + case ILInstructionAnd: |
| 58 | +#if 0 |
| 59 | + x86_emit1(&gd->ctx, flds, offset(get_operand(insn->reg[1]))); |
| 60 | + x86_emit1(&gd->ctx, fistp, eax); |
| 61 | + x86_emit1(&gd->ctx, flds, offset(get_operand(insn->reg[2]))); |
| 62 | + x86_emit1(&gd->ctx, fistp, ebx); |
| 63 | + x86_emit2(&gd->ctx, and, eax, ebx); |
| 64 | + x86_emit1(&gd->ctx, filds, eax); |
| 65 | + x86_emit1(&gd->ctx, fstps, offset(get_operand(insn->reg[0]))); |
| 66 | +#endif |
| 67 | + break; |
| 68 | + |
| 69 | + default: |
| 70 | + break; |
| 71 | + } |
| 72 | + |
| 73 | + return 0; |
| 74 | +} |
| 75 | + |
| 76 | +IL_CORE_COMPILE(avs_x86_compiler_compile) |
| 77 | +{ |
| 78 | + X86GlobalData *gd = X86_GLOBALDATA(ctx); |
| 79 | + ILInstruction *insn; |
| 80 | + |
| 81 | + avs_debug(print("X86: Compiling started...")); |
| 82 | + x86_context_reset(&gd->ctx); |
| 83 | + |
| 84 | + /* Compile function entrance, setup stack frame*/ |
| 85 | + x86_emit1(&gd->ctx, pushl, ebp); |
| 86 | + x86_emit2(&gd->ctx, movl, esp, ebp); |
| 87 | + |
| 88 | + for (insn=tree->base; insn != NULL; insn = insn->next) { |
| 89 | + avs_debug(print("X86: Compiling instruction: %p", insn)); |
| 90 | + compile_opcode(gd, insn); |
| 91 | + } |
| 92 | + |
| 93 | + /* Cleanup stack frame */ |
| 94 | + x86_emit0(&gd->ctx, emms); |
| 95 | + x86_emit0(&gd->ctx, leave); |
| 96 | + x86_emit0(&gd->ctx, ret); |
| 97 | + |
| 98 | + /* Link machine */ |
| 99 | + obj->run = (AvsRunnableExecuteCall) gd->ctx.buf; |
| 100 | + avs_debug(print("X86: Compiling finished...")); |
| 101 | + return 0; |
| 102 | +} |
| 103 | + |
| 104 | +IL_CORE_INIT(avs_x86_compiler_init) |
| 105 | +{ |
| 106 | + X86GlobalData *gd = visual_mem_malloc0(sizeof(X86GlobalData)); |
| 107 | + memset(gd, 0, sizeof(X86GlobalData)); |
| 108 | + ctx->ctx = gd; |
| 109 | + |
| 110 | + /* Initialize X86 Assembler opcode context */ |
| 111 | + x86_context_init(&gd->ctx, 4096, 1024*1024); |
| 112 | + |
| 113 | + return 0; |
| 114 | +} |
| 115 | + |
| 116 | +ILCore il_core_x86 = |
| 117 | +{ |
| 118 | + .name = "X86 Bytecode", |
| 119 | + .init = avs_x86_compiler_init, |
| 120 | + .compile = avs_x86_compiler_compile, |
| 121 | +}; |
| 122 | + |
0 commit comments