compiler

Unnamed repository; edit this file 'description' to name the repository.
git clone https://git.deepztream.com/compiler
Log | Files | Refs

commit 56b3154a739b979d92f86334d9785296ac252c6e
parent c67390f2b74b07d576675fd6165e2132af0b79c4
Author: William Djupström <william@deepztream.com>
Date:   Fri,  1 Mar 2019 13:29:41 +0100

Fixed add regression

Diffstat:
Mparse.y | 40++++++++++++++++++++++++----------------
1 file changed, 24 insertions(+), 16 deletions(-)

diff --git a/parse.y b/parse.y @@ -635,14 +635,15 @@ int compile_tree(FILE *output, struct expression *tree, int discard_result, int } break; case add: - j = compile_tree(output, tree->args, discard_result, out_reg); - k = compile_tree(output, &tree->args[1], discard_result, -1); + j = compile_tree(output, tree->args, discard_result, get_free_reg(1)); + k = compile_tree(output, &tree->args[1], discard_result, get_free_reg(1)); if (discard_result) { - if (j < registers) used_regs &= ~(1 << j); - if (k < registers) used_regs &= ~(1 << k); + if (j >= 0 && j < registers) used_regs &= ~(1 << j); + if (k >= 0 && k < registers) used_regs &= ~(1 << k); break; } reg = j; + /* if (reg >= registers) { reg = get_free_reg(1); } @@ -652,16 +653,19 @@ int compile_tree(FILE *output, struct expression *tree, int discard_result, int machine_code[mc_ptr++] = 0x45 + (reg % 8) * 8; machine_code[mc_ptr++] = -regs[j].rbp; } + */ machine_code[mc_ptr++] = 0x48 | (reg > 7 ? 4 : 0); machine_code[mc_ptr++] = 0x03; + /* if (tree->args[1].type == idnt) { machine_code[mc_ptr++] = 0x45 + (reg % 8) * 8; machine_code[mc_ptr++] = -regs[k].rbp; } else { + */ machine_code[mc_ptr-2] |= k > 7 ? 1 : 0; machine_code[mc_ptr++] = 0xC0 + (reg % 8) * 8 + k; - } - if (k < registers) used_regs &= ~(1 << k); + //} + if (k >= 0 && k < registers) used_regs &= ~(1 << k); break; case neg: j = compile_tree(output, tree->args, discard_result, out_reg); @@ -1746,7 +1750,7 @@ char padding[4096] = { 0 }; int main(int argc, char **argv) { - int i, outfd; + int i, outfd, use_rodata = 0; if (argc < 3) { fprintf(stderr, "Too few arguments\nUsage: %s <in-file> <out-file>\n", *argv); @@ -1765,8 +1769,10 @@ int main(int argc, char **argv) stringify(final, -3, 0, NULL, 0); - elf_hdr.phcount = 2; - elf_hdr.shcount = 4; + if (use_rodata) elf_hdr.phcount = 2; + else elf_hdr.phcount = 1; + if (use_rodata) elf_hdr.shcount = 4; + else elf_hdr.shcount = 3; elf_hdr.shstrindx = elf_hdr.shcount - 1; int hdr1_size = sizeof(elf_hdr) + sizeof(struct prg_hdr) * elf_hdr.phcount; @@ -1781,15 +1787,16 @@ int main(int argc, char **argv) exit(1); } + int section_pad = 16 - ((hdr1_size + mc_ptr) % 16); int rodata_offset = sizeof(elf_hdr) + sizeof(load_text) + sizeof(load_rodata) - + mc_ptr + sizeof(null_section) + sizeof(text_section) + + mc_ptr + section_pad + sizeof(null_section) + sizeof(text_section) + sizeof(rodata_section) + sizeof(strtab_section) + sizeof(strtab); int rodata_pad_len = 4096 - (rodata_offset % 4096); rodata_offset += rodata_pad_len; elf_hdr.entry = start_addr + entry_addr; - elf_hdr.shoff = hdr1_size + mc_ptr; + elf_hdr.shoff = hdr1_size + mc_ptr + section_pad; load_text.offset = 0; load_text.vaddr = 0x400000; @@ -1811,18 +1818,19 @@ int main(int argc, char **argv) rodata_section.offset = load_rodata.offset; rodata_section.size = load_rodata.fsize; - strtab_section.offset = hdr1_size + mc_ptr + sizeof(struct sec_hdr) * elf_hdr.shcount; + strtab_section.offset = hdr1_size + mc_ptr + section_pad + sizeof(struct sec_hdr) * elf_hdr.shcount; strtab_section.size = sizeof(strtab); write(outfd, &elf_hdr, sizeof(elf_hdr)); write(outfd, &load_text, sizeof(load_text)); - write(outfd, &load_rodata, sizeof(load_rodata)); + if (use_rodata) write(outfd, &load_rodata, sizeof(load_rodata)); write(outfd, machine_code, mc_ptr); + write(outfd, padding, section_pad); write(outfd, &null_section, sizeof(null_section)); write(outfd, &text_section, sizeof(text_section)); - write(outfd, &rodata_section, sizeof(rodata_section)); + if (use_rodata) write(outfd, &rodata_section, sizeof(rodata_section)); write(outfd, &strtab_section, sizeof(strtab_section)); write(outfd, strtab, sizeof(strtab)); - write(outfd, padding, rodata_pad_len); - write(outfd, rodata, rd_ptr); + if (use_rodata) write(outfd, padding, rodata_pad_len); + if (use_rodata) write(outfd, rodata, rd_ptr); }