arm.cpp 110.10 KiB
#include "arm.h"
#include "Module.h"
#include<cmath>
using namespace std;
int vreg_count;
int vsreg_count;
float2int f2i;
bool opt=false;
int ori_func_stack=0;
// int offset=0;
static Map<ValuePtr, MOperand> value_map; 
static Map<ValuePtr, MOperand> float_value_map; 
static Map<MOperand, MOperand> imm_map; 
static Map<ValuePtr, int> ptr_val_map; 
static map<MOperand, int> int_val_map;
static map<string,Binary_Op_Type> Binary_ir2asm;
static map<ValuePtr,ValuePtr> my_arr_base;
static map<ValuePtr,int> stack_val_map;
ValuePtr cur_arr_base;
MOperand make_imm(int32 constant)       { return MOperand(IMM, constant); }
MOperand make_reg(uint8 reg)            { return MOperand(REG, reg); }
MOperand make_simm(float constant)      { return MOperand(SIMM, constant); }
MOperand make_vreg(int32 vreg_index)    { return MOperand(VREG, vreg_index); }
MOperand make_sreg(uint8 sreg)          { return MOperand(SREG, sreg); }
MOperand make_vsreg(int32 vsreg_index)  { return MOperand(VSREG, vsreg_index); }
//this is used to put the later args(e.g. last four) on the stack into the func
// @todo config it well
void emit_load_of_later_arg(ValuePtr a, MOperand vreg, Machine_Block *mb, int idx) {
    // assert(idx >= 4);
    auto offset_relative_to_fp = (idx - 4) * 4;
    auto ldr = new MI_Load;
    ldr->mem_tag = MEM_LOAD_ARG;
    ldr->reg = vreg;
    ldr->base = make_reg(sp);
    ldr->offset = make_imm(offset_relative_to_fp);
    if (mb->control_transfer_inst) {
        insert(ldr, mb->control_transfer_inst);
    } else {
        push(ldr, mb);
// this is the function to get the stack value
// because we put all the int into the stack
MOperand get_stack_val(ValuePtr v, Machine_Block* mb,Func_Asm *func_asm)
    int val=0;
    if(ori_func_stack!=0)
        val=ori_func_stack-stack_val_map[v];
    else 
        val=func_asm->stack_size-stack_val_map[v];
    auto bi_I = new MI_Binary;
    auto nreg=make_vreg(vreg_count++);
	bi_I->op = BINARY_ADD;
	bi_I->lhs = make_reg(sp);
	bi_I->rhs = make_imm(val);
	bi_I->dst = nreg;
	push((MI *)bi_I, mb);
    return bi_I->dst;
// insert the first MI before the second