diff options
| -rw-r--r-- | main.c | 41 | ||||
| -rw-r--r-- | test/test.sh | 40 |
2 files changed, 45 insertions, 36 deletions
@@ -307,20 +307,7 @@ static void assembleplusr(uint8_t opcode, uint8_t rexw, AsmKind reg) { sb(opcode | (bits & 7)); } -/* Assemble a symbolic value. */ -static void assemblereloc(const char *l, int64_t c, int nbytes, int type) { - Relocation *reloc; - Symbol *sym; - - if (l != NULL) { - reloc = newreloc(); - sym = getsym(l); - reloc->type = type; - reloc->section = cursection; - reloc->sym = sym; - reloc->offset = cursection->hdr.sh_size; - } - +void assembleconstant(int64_t c, int nbytes) { switch (nbytes) { case 1: sb((uint8_t)c); @@ -332,24 +319,46 @@ static void assemblereloc(const char *l, int64_t c, int nbytes, int type) { su32((uint32_t)c); break; case 8: - fatal("TODO 8 byte symbolic"); + fatal("TODO 8 byte"); break; default: unreachable(); } } +/* Assemble a symbolic value. */ +static void assemblereloc(const char *l, int64_t c, int nbytes, int type) { + Relocation *reloc; + Symbol *sym; + + if (l != NULL) { + reloc = newreloc(); + sym = getsym(l); + reloc->type = type; + reloc->section = cursection; + reloc->sym = sym; + reloc->offset = cursection->hdr.sh_size; + } + assembleconstant(c, nbytes); +} + /* Assemble a r <-> (%rip) operation. */ static void assembleriprel(Memarg *memarg, uint8_t rexw, uint8_t opcode, uint8_t reg, uint8_t opsz) { uint8_t rex; + if (opsz == 2) sb(0x66); rex = rexbyte(rexw, 0, 0, 0); if (rex != rexbyte(0, 0, 0, 0)) sb(rex); sb2(opcode, modregrm(0x00, reg, 0x05)); - assemblereloc(memarg->l, memarg->c - 4, 4, R_X86_64_PC32); + + if (memarg->l) { + assemblereloc(memarg->l, memarg->c - 4, 4, R_X86_64_PC32); + } else { + assembleconstant(memarg->c, 4); + } } /* Assemble a r <-> mem operation. */ diff --git a/test/test.sh b/test/test.sh index 7fbd9f5..cf5660b 100644 --- a/test/test.sh +++ b/test/test.sh @@ -8,26 +8,6 @@ tmpb="$(mktemp --suffix .bin)" trap "rm -f \"$tmps\" \"$tmpo\" \"$tmpb\"" EXIT t () { - if ! ./minias < "$1" > "$tmpo" - then - echo "failed to assemble: $1" - exit 1 - fi - clang "$tmpo" -o "$tmpb" - if !"$tmpb" 1>&2 2>/dev/null - then - echo "$t failed" - exit 1 - fi - echo -n "." -} - -for tc in $(echo test/execute/*) -do - t "$tc" -done - -t () { echo "$1" > "$tmps" clang -Wno-everything -c -s "$tmps" -o "$tmpo" objcopy -j ".text" -O binary "$tmpo" "$tmpb" @@ -138,3 +118,23 @@ do t "${op}q %r${r}x, %rax" done done + +t () { + if ! ./minias < "$1" > "$tmpo" + then + echo "failed to assemble: $1" + exit 1 + fi + clang "$tmpo" -o "$tmpb" + if !"$tmpb" 1>&2 2>/dev/null + then + echo "$t failed" + exit 1 + fi + echo -n "." +} + +for tc in $(echo test/execute/*) +do + t "$tc" +done
\ No newline at end of file |
