Add Disassemble, fix bug in position calculation

* Disassemble structure can be used for dumping an object section
* Assembler position calculation was messing up, causing jump addresses
  to be wrong. This is fixed.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2020-02-10 16:31:08 -05:00
parent 6c352396fa
commit a4a37b5a27
8 changed files with 397 additions and 71 deletions

View File

@@ -60,7 +60,6 @@ impl<'a> Assemble<'a> {
for block in self.ast.iter() {
let locals = Self::gather_symbols(block, false)?;
self.symbols.replace_locals(locals);
match block {
SectionBlock::Data { org, body } | SectionBlock::Code { org, body } => {
let mut bytes = Vec::new();
@@ -191,19 +190,20 @@ impl<'a> Assemble<'a> {
Inst::Jnz(r1) => builder.op(JNZ).r1(*r1),
Inst::Load(r1, r2) => builder.op(LOAD).r1(*r1).r2(*r2),
Inst::Store(r1, r2) => builder.op(STORE).r1(*r1).r2(*r2),
Inst::StoreImm(r1, imm) => {
let imm = match imm {
ImmValue::Number(num) => *num,
ImmValue::Label(name) => {
self.symbols.get(name).expect("TODO: value label not found")
Inst::StoreImm(r1, imm) => match imm {
ImmValue::Number(num) => {
if *num > (u32::max_value() as u64) {
builder.op(STOREIMM64).imm64(*num)
} else {
builder.op(STOREIMM32).imm32(*num as u32)
}
};
if imm <= (u32::max_value() as u64) {
builder.op(STOREIMM32).r1(*r1).imm32(imm as u32)
} else {
builder.op(STOREIMM64).r1(*r1).imm64(imm)
}
ImmValue::Label(name) => {
let imm = self.symbols.get(name).expect("TODO: value label not found");
builder.op(STOREIMM64).imm64(imm)
}
}
.r1(*r1),
Inst::MemCopy(r1, r2) => builder.op(MEMCOPY).r1(*r1).r2(*r2),
Inst::RegCopy(r1, r2) => builder.op(REGCOPY).r1(*r1).r2(*r2),
Inst::Nop => builder.op(NOP),