pass-lang/src/asm.c

44 lines
1.2 KiB
C
Raw Normal View History

/// This file handles the contextual generation of machine code.
/// It abstracts over quirks like the limitations of addressing modes,
/// provides higher-level functionality, and can perform peephole optimization.
2022-09-05 23:48:56 -07:00
#include "asm.h"
#include "format.h"
#include "x86encode.h"
2022-09-05 23:48:56 -07:00
#include <stdlib.h>
#include <string.h>
void inst_jump(symbol sym) {
int32_t disp = symbol_offset(sym, X86_JMP_DISP8_SIZE);
if (disp >= INT8_MIN && disp <= INT8_MAX) {
x86_inst_jmp_disp8(disp);
} else {
x86_inst_jmp_disp32_op();
relocate_pc32(sym);
}
// TODO: support 64-bit jumps?
2022-09-05 23:48:56 -07:00
}
void inst_jump_if_zero(symbol sym, reg reg) {
x86_inst_test_r64_r64(reg, reg);
int32_t disp = symbol_offset(sym, X86_JZ_DISP8_SIZE);
if (disp >= INT8_MIN && disp <= INT8_MAX) {
x86_inst_jz_disp8(disp);
} else {
x86_inst_jz_disp32_op();
relocate_pc32(sym);
}
}
void inst_jump_if_not_zero(symbol sym, reg reg) {
x86_inst_test_r64_r64(reg, reg);
int32_t disp = symbol_offset(sym, X86_JNZ_DISP8_SIZE);
if (disp >= INT8_MIN && disp <= INT8_MAX) {
x86_inst_jnz_disp8(disp);
} else {
x86_inst_jnz_disp32_op();
relocate_pc32(sym);
}
}