Skip to content

Commit

Permalink
Fix assembly
Browse files Browse the repository at this point in the history
  • Loading branch information
tyfkda committed Apr 17, 2024
1 parent a3d650c commit de29084
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 18 deletions.
12 changes: 6 additions & 6 deletions src/as/arch/aarch64/asm_code.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@
#endif

void make_code16(Inst *inst, Code *code, unsigned short *buf, int len) {
assert(len <= (int)sizeof(code->buf));
assert(code->len + len <= (int)sizeof(code->buf));
code->inst = inst;
code->len = len;
memcpy(code->buf, buf, len);
memcpy(code->buf + code->len, buf, len);
code->len += len;
}

void make_code32(Inst *inst, Code *code, unsigned int *buf, int len) {
assert(len <= (int)sizeof(code->buf));
assert(code->len + len <= (int)sizeof(code->buf));
code->inst = inst;
code->len = len;
memcpy(code->buf, buf, len);
memcpy(code->buf + code->len, buf, len);
code->len += len;
}

inline bool assemble_error(const ParseInfo *info, const char *message) {
Expand Down
16 changes: 7 additions & 9 deletions src/as/arch/riscv64/asm_code.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@
#endif

void make_code16(Inst *inst, Code *code, unsigned short *buf, int len) {
assert(len <= (int)sizeof(code->buf));
assert(code->len + len <= (int)sizeof(code->buf));
code->inst = inst;
code->len = len;
memcpy(code->buf, buf, len);
memcpy(code->buf + code->len, buf, len);
code->len += len;
}

void make_code32(Inst *inst, Code *code, unsigned int *buf, int len) {
assert(len <= (int)sizeof(code->buf));
assert(code->len + len <= (int)sizeof(code->buf));
code->inst = inst;
code->len = len;
memcpy(code->buf, buf, len);
memcpy(code->buf + code->len, buf, len);
code->len += len;
}

inline bool is_im6(int64_t x) {
Expand Down Expand Up @@ -65,7 +65,7 @@ inline bool assemble_error(const ParseInfo *info, const char *message) {
#define C_LUI(rd, imm) MAKE_CODE16(inst, code, 0x6001 | (IMM(imm, 5, 5) << 12) | (rd << 7) | (IMM(imm, 4, 0) << 2))
#define C_ADDI(rd, imm) MAKE_CODE16(inst, code, 0x0001 | (IMM(imm, 5, 5) << 12) | (rd << 7) | (IMM(imm, 4, 0) << 2))
#define C_ADDIW(rd, imm) MAKE_CODE16(inst, code, 0x2001 | (IMM(imm, 5, 5) << 12) | (rd << 7) | (IMM(imm, 4, 0) << 2))
#define C_LDSP(rd, imm) MAKE_CODE16(inst, code, 0xe002 | (IMM(imm, 5, 5) << 12) | (rd << 7) | (IMM(imm, 4, 3) << 5) | (IMM(imm, 8, 6) << 2))
#define C_LDSP(rd, imm) MAKE_CODE16(inst, code, 0x6002 | (IMM(imm, 5, 5) << 12) | (rd << 7) | (IMM(imm, 4, 3) << 5) | (IMM(imm, 8, 6) << 2))
#define C_SDSP(rs, imm) MAKE_CODE16(inst, code, 0xe002 | (IMM(imm, 5, 3) << 10) | (IMM(imm, 8, 6) << 7) | (rs << 2))
#define C_JR(rs) MAKE_CODE16(inst, code, 0x8002 | (rs << 7))

Expand Down Expand Up @@ -125,7 +125,6 @@ static unsigned char *asm_ld(Inst *inst, Code *code) {
int64_t imm = offset != NULL ? offset->fixnum : 0;
int base_reg = inst->opr2.indirect.reg.no;
if (imm >= 0 && imm < (1 << 9) && (imm & 7) == 0 && base_reg == SP) {
imm >>= 3;
C_LDSP(rd, imm);
return code->buf;
}
Expand All @@ -140,7 +139,6 @@ static unsigned char *asm_sd(Inst *inst, Code *code) {
int64_t imm = offset != NULL ? offset->fixnum : 0;
int base_reg = inst->opr2.indirect.reg.no;
if (imm >= 0 && imm < (1 << 9) && (imm & 7) == 0 && base_reg == SP) {
imm >>= 3;
C_SDSP(rd, imm);
return code->buf;
}
Expand Down
6 changes: 3 additions & 3 deletions src/as/arch/x64/asm_code.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ static unsigned char *put_code_filtered(unsigned char *p, const short *buf, size
}

void make_code(Inst *inst, Code *code, unsigned char *buf, int len) {
assert(len <= (int)sizeof(code->buf));
assert(code->len + len <= (int)sizeof(code->buf));
code->inst = inst;
code->len = len;
memcpy(code->buf, buf, len);
memcpy(code->buf + code->len, buf, len);
code->len += len;
}

inline char opr_regno(const Reg *reg) {
Expand Down
1 change: 1 addition & 0 deletions src/as/arch/x64/ir_asm.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ static bool make_jmp_long(IR *ir) {
Inst *inst = ir->code.inst;
// Change to long offset, and recalculate.
ir->code.flag |= INST_LONG_OFFSET;
ir->code.len = 0;
if (inst->op == JMP)
MAKE_CODE(inst, &ir->code, 0xe9, IM32(-1));
else
Expand Down

0 comments on commit de29084

Please sign in to comment.