From 3f1492947cbf50ebb8e5d79f3bbdb06b85443abe Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Sat, 11 Jun 2022 22:52:13 +0200 Subject: [PATCH] Implement symbol table for functions --- Rakefile | 2 +- source/elna/backend.d | 25 ++- source/elna/elf.d | 433 +++++++++++++++++++++++++----------------- source/elna/result.d | 22 +++ source/elna/riscv.d | 107 +++++++---- 5 files changed, 371 insertions(+), 218 deletions(-) diff --git a/Rakefile b/Rakefile index 4dfb642..bb4e232 100644 --- a/Rakefile +++ b/Rakefile @@ -26,7 +26,7 @@ rule(/build\/riscv\/[^\/\.]+$/ => ->(file) { test_for_out(file, '.o') }) do |t| '/opt/riscv/riscv32-unknown-elf/lib/crt0.o', '/opt/riscv/lib/gcc/riscv32-unknown-elf/11.1.0/crtbegin.o', t.source, - '--start-group', '-lc', '-lgloss', '--end-group', + '--start-group', '-lgcc', '-lc', '-lgloss', '--end-group', '/opt/riscv/lib/gcc/riscv32-unknown-elf/11.1.0/crtend.o' end diff --git a/source/elna/backend.d b/source/elna/backend.d index c7cb2ca..33914de 100644 --- a/source/elna/backend.d +++ b/source/elna/backend.d @@ -7,6 +7,7 @@ import elna.extended; import elna.riscv; import elna.lexer; import elna.parser; +import elna.result; import std.algorithm; import std.sumtype; import std.typecons; @@ -55,9 +56,27 @@ int generate(string inFile, ref String outputFilename) @nogc { return 1; } - auto programText = writeNext(ir); - auto elf = Elf(move(handle)); - elf.addCode("main", programText); + auto program = writeNext(ir); + auto elf = Elf!ELFCLASS32(move(handle)); + auto readOnlyData = Array!ubyte(cast(const(ubyte)[]) "%d".ptr[0 .. 3]); // With \0. + Array!Relocation relocationData; + + foreach (ref reference; program.symbols) + { + Relocation relocationEntry; + + relocationEntry.symbol = reference; + relocationEntry.typeInformation = R_RISCV_CALL; + relocationEntry.hasEntry = true; + relocationData.insertBack(relocationEntry); + + relocationEntry.typeInformation = R_RISCV_RELAX; + relocationEntry.hasEntry = false; + relocationData.insertBack(relocationEntry); + } + + elf.addReadOnlyData(readOnlyData); + elf.addCode(program.name, program.text, relocationData); elf.finish(); diff --git a/source/elna/elf.d b/source/elna/elf.d index b0f6d2c..9aa2806 100644 --- a/source/elna/elf.d +++ b/source/elna/elf.d @@ -1,12 +1,13 @@ module elna.elf; import elna.extended; +import elna.result; import std.algorithm; import tanya.container.array; import tanya.container.string; /// Unsigned program address. -alias Elf64_Addr = ulong*; +alias Elf64_Addr = ulong; /// Unsigned file offset. alias Elf64_Off = ulong; /// Unsigned medium integer. @@ -336,7 +337,7 @@ auto ELF64_R_TYPE(I)(I i) return i & 0xffffffffL; } -auto ELF64_R_INFO(S, T)(S s, t) +Elf64_Word ELF64_R_INFO(S)(S s, ubyte t) { return (s << 32) + (t & 0xffffffffL); } @@ -356,7 +357,7 @@ ubyte ELF32_ST_INFO(ubyte b, ubyte t) @nogc nothrow pure @safe return cast(ubyte) ((b << 4) + (t & 0xf)); } -T ELF32_R_SYMT(I)(I i) +auto ELF32_R_SYMT(I)(I i) { return i >> 8; } @@ -366,9 +367,9 @@ ubyte ELF32_R_TYPE(I)(I i) return cast(ubyte) i; } -auto ELF32_R_INFO(S, T)(S s, T t) +Elf32_Word ELF32_R_INFO(S)(S s, ubyte t) { - return (s << 8) + cast(ubyte) t; + return cast(Elf32_Word) ((s << 8) + t); } enum : uint @@ -407,153 +408,6 @@ enum : uint STT_HIPROC = 15, } -Elf64_Ehdr makeFileHeader(Elf64_Off sectionHeaderOffset, - Elf64_Half sectionHeaderCount, - Elf64_Half stringIndex) @nogc -{ - Elf64_Ehdr header; - - // Magic number. - header.e_ident[0] = '\x7f'; - header.e_ident[1] = 'E'; - header.e_ident[2] = 'L'; - header.e_ident[3] = 'F'; - - // File class. - header.e_ident[4] = ELFCLASS64; - - // Data encoding. - header.e_ident[5] = ELFDATA2LSB; - - // Version. - header.e_ident[6] = EV_CURRENT; - - // OS/ABI identification. - header.e_ident[7] = EI_OSABI.ELFOSABI_SYSV; - - // ABI version. - header.e_ident[8] = 0; - - // Size of e_ident[]. - header.e_ident[15] = 0; - - header.e_type = ET_REL; - header.e_machine = 0x3e; // EM_X86_64: AMD x86-64 architecture - header.e_version = EV_CURRENT; - header.e_entry = null; - header.e_phoff = 0; - header.e_shoff = sectionHeaderOffset; - header.e_flags = 0; - header.e_ehsize = Elf64_Ehdr.sizeof; - header.e_phentsize = 0; - header.e_phnum = 0; - header.e_shentsize = Elf64_Shdr.sizeof; - header.e_shnum = sectionHeaderCount; - header.e_shstrndx = stringIndex; - - return header; -} - -Elf64_Shdr makeTextHeader(Elf64_Off offset, Elf64_Xword size) @nogc -{ - Elf64_Shdr table; - - table.sh_name = 0x1b; - table.sh_type = SHT_PROGBITS; - table.sh_flags = SHF_EXECINSTR | SHF_ALLOC; - table.sh_addr = null; - table.sh_offset = offset; - table.sh_size = size; - table.sh_link = SHN_UNDEF; - table.sh_info = 0; - table.sh_addralign = 1; - table.sh_entsize = 0; - - return table; -} - -Elf64_Shdr makeSymtableHeader(Elf64_Off offset, Elf64_Xword size, Elf64_Word entriesCount) @nogc -{ - Elf64_Shdr table; - - table.sh_name = 0x01; - table.sh_type = SHT_SYMTAB; - table.sh_flags = 0; - table.sh_addr = null; - table.sh_offset = offset; - table.sh_size = size; - table.sh_link = 0x03; // String table used by entries in this section. - table.sh_info = entriesCount; - table.sh_addralign = 8; - table.sh_entsize = Elf64_Sym.sizeof; - - return table; -} - -Elf64_Shdr makeStringHeader(Elf64_Word stringIndex, Elf64_Off offset, Elf64_Xword size) @nogc -{ - Elf64_Shdr table; - - table.sh_name = stringIndex; - table.sh_type = SHT_STRTAB; - table.sh_flags = 0; - table.sh_addr = null; - table.sh_offset = offset; - table.sh_size = size; - table.sh_link = SHN_UNDEF; - table.sh_info = 0; - table.sh_addralign = 1; - table.sh_entsize = 0; - - return table; -} - -Elf64_Shdr makeInitialHeader() @nogc -{ - Elf64_Shdr table; - - table.sh_name = 0; - table.sh_type = SHT_NULL; - table.sh_flags = 0; - table.sh_addr = null; - table.sh_offset = 0; - table.sh_size = 0; - table.sh_link = SHN_UNDEF; - table.sh_info = 0; - table.sh_addralign = 0; - table.sh_entsize = 0; - - return table; -} - -Elf64_Sym makeInitialSymTable() @nogc -{ - Elf64_Sym table; - - table.st_name = 0; - table.st_info = 0; - table.st_other = 0; - table.st_shndx = 0; - table.st_value = null; - table.st_size = 0; - - return table; -} - -Elf64_Sym makeMainSymTable(Elf64_Half textIndex) @nogc -{ - Elf64_Sym table; - - table.st_name = 0x01; - table.st_info = ELF32_ST_INFO(STB_GLOBAL, STT_FUNC); - table.st_other = 0; - table.st_shndx = textIndex; - table.st_value = null; - table.st_size = 0; - - return table; -} - /// Special Section Indices. enum : ushort { @@ -633,6 +487,105 @@ enum : Elf64_Half ET_HIPROC = 0xFFFF, } +enum : ubyte +{ + R_RISCV_NONE = 0, + /// 32-bit relocation. + R_RISCV_32 = 1, + /// 64-bit relocation. + R_RISCV_64 = 2, + /// Relocation against a local symbol in a shared object. + R_RISCV_RELATIVE = 3, + /// Must be in executable; not allowed in shared library. + R_RISCV_COPY = 4, + /// Indicates the symbol associated with a PLT entry. + R_RISCV_JUMP_SLOT = 5, + R_RISCV_TLS_DTPMOD32 = 6, + R_RISCV_TLS_DTPMOD64 = 7, + R_RISCV_TLS_DTPREL32 = 8, + R_RISCV_TLS_DTPREL64 = 9, + R_RISCV_TLS_TPREL32 = 10, + R_RISCV_TLS_TPREL64 = 11, + /// 12-bit PC-relative branch offset. + R_RISCV_BRANCH = 16, + /// 20-bit PC-relative jump offset. + R_RISCV_JAL = 17, + /// 32-bit PC-relative function call, macros `call`, `tail`. + R_RISCV_CALL = 18, + /// 32-bit PC-relative function call, macros `call`, `tail` (PIC). + R_RISCV_CALL_PLT = 19, + /// High 20 bits of 32-bit PC-relative GOT access, `%got_pcrel_hi(symbol)`. + R_RISCV_GOT_HI20 = 20, + /// High 20 bits of 32-bit PC-relative TLS IE GOT access, macro `la.tls.ie`. + R_RISCV_TLS_GOT_HI20 = 21, + /// High 20 bits of 32-bit PC-relative TLS GD GOT reference, macro `la.tls.gd`. + R_RISCV_TLS_GD_HI20 = 22, + /// High 20 bits of 32-bit PC-relative reference, `%pcrel_hi(symbol)`. + R_RISCV_PCREL_HI20 = 23, + /// Low 12 bits of a 32-bit PC-relative, `%pcrel_lo(address of %pcrel_hi)`, the addend must be 0. + R_RISCV_PCREL_LO12_I = 24, + /// Low 12 bits of a 32-bit PC-relative, `%pcrel_lo(address of %pcrel_hi)`, the addend must be 0. + R_RISCV_PCREL_LO12_S = 25, + /// High 20 bits of 32-bit absolute address, `%hi(symbol)`. + R_RISCV_HI20 = 26, + /// Low 12 bits of 32-bit absolute address, `%lo(symbol)`. + R_RISCV_LO12_I = 27, + /// Low 12 bits of 32-bit absolute address, `%lo(symbol)`. + R_RISCV_LO12_S = 28, + /// High 20 bits of TLS LE thread pointer offset, `%tprel_hi(symbol)`. + R_RISCV_TPREL_HI20 = 29, + /// Low 12 bits of TLS LE thread pointer offset, `%tprel_lo(symbol)`. + R_RISCV_TPREL_LO12_I = 30, + /// Low 12 bits of TLS LE thread pointer offset, `%tprel_lo(symbol)`. + R_RISCV_TPREL_LO12_S = 31, + /// TLS LE thread pointer usage, `%tprel_add(symbol)`. + R_RISCV_TPREL_ADD = 32, + /// 8-bit label addition. + R_RISCV_ADD8 = 33, + /// 16-bit label addition. + R_RISCV_ADD16 = 34, + /// 32-bit label addition. + R_RISCV_ADD32 = 35, + /// 64-bit label addition. + R_RISCV_ADD64 = 36, + /// 8-bit label subtraction. + R_RISCV_SUB8 = 37, + /// 16-bit label subtraction. + R_RISCV_SUB16 = 38, + /// 32-bit label subtraction. + R_RISCV_SUB32 = 39, + /// 64-bit label subtraction. + R_RISCV_SUB64 = 40, + /// GNU {Cpp} vtable hierarchy. + R_RISCV_GNU_VTINHERIT = 41, + /// GNU {Cpp} vtable member usage. + R_RISCV_GNU_VTENTRY = 42, + /// Alignment statement. + R_RISCV_ALIGN = 43, + /// 8-bit PC-relative branch offset. + R_RISCV_RVC_BRANCH = 44, + /// 11-bit PC-relative jump offset. + R_RISCV_RVC_JUMP = 45, + /// High 6 bits of 18-bit absolute address. + R_RISCV_RVC_LUI = 46, + /// Instruction can be relaxed, paired with a normal relocation at the same address. + R_RISCV_RELAX = 51, + /// Local label subtraction. + R_RISCV_SUB6 = 52, + /// Local label assignment. + R_RISCV_SET6 = 53, + /// Local label assignment. + R_RISCV_SET8 = 54, + /// Local label assignment. + R_RISCV_SET16 = 55, + /// Local label assignment. + R_RISCV_SET32 = 56, + /// 32-bit PC relative. + R_RISCV_32_PCREL = 57, + /// Relocation against a local ifunc symbol in a shared object. + R_RISCV_IRELATIVE = 58 +} + auto pad(ubyte elfClass)(size_t value) @nogc { static if (elfClass == ELFCLASS32) @@ -649,30 +602,76 @@ auto pad(ubyte elfClass)(size_t value) @nogc } } -struct Symbol +struct Elf(ubyte elfClass) { - String name; - const(ubyte)[] text; -} + static if (elfClass == ELFCLASS32) + { + alias Addr = Elf32_Addr; + alias Off = Elf32_Off; + alias Half = Elf32_Half; + alias Word = Elf32_Word; + alias Sword = Elf32_Sword; + alias Xword = Elf32_Word; + alias Sxword = Elf32_Sword; + + alias Ehdr = Elf32_Ehdr; + alias Shdr = Elf32_Shdr; + alias Rel = Elf32_Rel; + alias Rela = Elf32_Rela; + alias Sym = Elf32_Sym; + + alias R_SYMT = ELF32_R_SYMT; + alias R_TYPE = ELF32_R_TYPE; + alias R_INFO = ELF32_R_INFO; + alias ST_BIND = ELF32_ST_BIND; + alias ST_TYPE = ELF32_ST_TYPE; + alias ST_INFO = ELF32_ST_INFO; + } + else static if (elfClass == ELFCLASS64) + { + alias Addr = Elf64_Addr; + alias Off = Elf64_Off; + alias Half = Elf64_Half; + alias Word = Elf64_Word; + alias Sword = Elf64_Sword; + alias Xword = Elf64_Xword; + alias Sxword = Elf64_Sxword; + + alias Ehdr = Elf64_Ehdr; + alias Shdr = Elf64_Shdr; + alias Rel = Elf64_Rel; + alias Rela = Elf64_Rela; + alias Sym = Elf64_Sym; + + alias R_SYMT = ELF64_R_SYMT; + alias R_TYPE = ELF64_R_TYPE; + alias R_INFO = ELF64_R_INFO; + alias ST_BIND = ELF64_ST_BIND; + alias ST_TYPE = ELF64_ST_TYPE; + alias ST_INFO = ELF64_ST_INFO; + } + else + { + static assert(false, "Invalid ELF class"); + } -struct Elf -{ private Elf32_Ehdr fileHeader; private Array!Elf32_Shdr sectionHeaders; private Elf32_Off currentOffset = Elf32_Ehdr.sizeof; private Array!Elf32_Sym symbols; - static immutable char[41] sections = - "\0.symtab\0.strtab\0.shstrtab\0.text\0.rodata\0"; + static immutable char[52] sections = + "\0.symtab\0.strtab\0.shstrtab\0.text\0.rodata\0.rela.text\0"; private String strings; - private Elf32_Word lastLocalSymbol; - private Elf32_Word textSize; + private Word lastLocalSymbol; + private Word textSize; private File output; + private Array!ubyte readOnly; + private Array!Rela relocations; static Elf opCall(File output) @nogc { Elf elf = Elf.init; - elf.initializeFileHeader(); elf.initializeSectionHeaders(); elf.insertSymbols(); elf.output = move(output); @@ -687,7 +686,9 @@ struct Elf void finish() @nogc { makeTextHeader(); - initializeSymbolTable(cast(Elf32_Word) (this.sectionHeaders.length + 1)); + makeRelaHeader(cast(Word) (this.sectionHeaders.length + 2), cast(Word) (this.sectionHeaders.length - 1)); + makeRoDataHeader(); + initializeSymbolTable(cast(Word) (this.sectionHeaders.length + 1)); foreach (symbol; this.symbols) { @@ -711,12 +712,7 @@ struct Elf output.write((cast(ubyte*) this.sectionHeaders.get)[0 .. Elf32_Shdr.sizeof * this.sectionHeaders.length]); - output.seek(0, File.Whence.set); - this.fileHeader.e_shoff = this.currentOffset; - this.fileHeader.e_shnum = cast(Elf32_Half) this.sectionHeaders.length; - // String table is the last one - this.fileHeader.e_shstrndx = cast(Elf32_Half) (this.sectionHeaders.length - 1); - output.write((cast(ubyte*) &this.fileHeader)[0 .. fileHeader.sizeof]); + this.initializeFileHeader(); } private void insertSymbols() @nogc @@ -752,7 +748,7 @@ struct Elf return table; } - private void initializeSymbolTable(Elf32_Word stringTableIndex) @nogc + private void initializeSymbolTable(Word stringTableIndex) @nogc { Elf32_Shdr symbolTableHeader; @@ -771,24 +767,61 @@ struct Elf this.sectionHeaders.insertBack(symbolTableHeader); } - void addCode(string name, ref Array!ubyte text) @nogc + void addCode(ref String name, ref Array!ubyte text, Array!Relocation usedSymbols) + @nogc { this.output.write(text.get); this.strings.insertBack("\0"); this.strings.insertBack(name[]); - Elf32_Sym symbol; + Sym symbol; // Main function symbol.st_name = 0x1; // Word symbol.st_value = 0; // Addr symbol.st_size = cast(Elf32_Word) text.length; // Word - symbol.st_info = ELF32_ST_INFO(STB_GLOBAL, STT_FUNC); // char + symbol.st_info = ST_INFO(STB_GLOBAL, STT_FUNC); // char symbol.st_other = 0; // char // .text header index, half word symbol.st_shndx = cast(Elf32_Half) this.sectionHeaders.length; this.symbols.insertBack(symbol); this.textSize += text.length; + + foreach (usedSymbol; usedSymbols) + { + Rela relocationEntry; + + relocationEntry.r_offset = cast(Addr) usedSymbol.symbol.offset; + if (usedSymbol.hasEntry) + { + relocationEntry.r_info = ELF32_R_INFO(this.symbols.length, usedSymbol.typeInformation); + this.relocations.insertBack(relocationEntry); + } + else + { + relocationEntry.r_info = ELF32_R_INFO(0, usedSymbol.typeInformation); + this.relocations.insertBack(relocationEntry); + continue; + } + Sym usedSymbolEntry; + + this.strings.insertBack("\0"); + usedSymbolEntry.st_name = cast(Word) this.strings.length; + usedSymbolEntry.st_value = 0; + usedSymbolEntry.st_size = 0; + usedSymbolEntry.st_info = ST_INFO(STB_GLOBAL, STT_NOTYPE); + usedSymbolEntry.st_other = 0; + usedSymbolEntry.st_shndx = SHN_UNDEF; + + this.strings.insertBack(usedSymbol.symbol.name[]); + this.strings.insertBack("\0"); + this.symbols.insertBack(usedSymbolEntry); + } + } + + void addReadOnlyData(ref Array!ubyte data) @nogc + { + this.readOnly.insertBack(data[]); } private void makeTextHeader() @nogc @@ -847,13 +880,59 @@ struct Elf this.fileHeader.e_version = EV_CURRENT; this.fileHeader.e_entry = 0; this.fileHeader.e_phoff = 0; - // this.fileHeader.e_shoff = ?; (section header offset) + this.fileHeader.e_shoff = this.currentOffset; this.fileHeader.e_flags = 0; this.fileHeader.e_ehsize = Elf32_Ehdr.sizeof; this.fileHeader.e_phentsize = 0; this.fileHeader.e_phnum = 0; this.fileHeader.e_shentsize = Elf32_Shdr.sizeof; - // this.fileHeader.e_shnum = ?; (section header count) - // this.fileHeader.e_shstrndx = ?; (string index) + this.fileHeader.e_shnum = cast(Elf32_Half) this.sectionHeaders.length; + + // String table is the last one + this.fileHeader.e_shstrndx = cast(Elf32_Half) (this.sectionHeaders.length - 1); + + output.seek(0, File.Whence.set); + output.write((cast(ubyte*) &this.fileHeader)[0 .. fileHeader.sizeof]); + } + + private void makeRoDataHeader() @nogc + { + Shdr table; + + table.sh_name = 0x21; + table.sh_type = SHT_PROGBITS; + table.sh_flags = SHF_ALLOC; + table.sh_addr = 0; + table.sh_offset = this.currentOffset; + table.sh_size = cast(Xword) this.readOnly.length; + table.sh_link = SHN_UNDEF; + table.sh_info = 0; + table.sh_addralign = 4; + table.sh_entsize = 0; + + output.write(this.readOnly.get); + + this.sectionHeaders.insertBack(table); + this.currentOffset += this.readOnly.length; + } + + private void makeRelaHeader(Word symTableIndex, Word targetTableIndex) @nogc + { + Shdr table; + + table.sh_name = 0x29; + table.sh_type = SHT_RELA; + table.sh_flags = this.relocations.length == 0 ? 0 : SHF_ALLOC; + table.sh_addr = 0; + table.sh_offset = this.currentOffset; + table.sh_size = cast(Word) (Rela.sizeof * this.relocations.length); + table.sh_link = symTableIndex; + table.sh_info = targetTableIndex; + table.sh_addralign = 4; + table.sh_entsize = Rela.sizeof; + + this.output.write((cast(ubyte*) this.relocations.get)[0 .. Rela.sizeof * this.relocations.length]); + this.currentOffset += Rela.sizeof * this.relocations.length; + this.sectionHeaders.insertBack(table); } } diff --git a/source/elna/result.d b/source/elna/result.d index 049c453..9ed62d1 100644 --- a/source/elna/result.d +++ b/source/elna/result.d @@ -1,6 +1,8 @@ module elna.result; import std.typecons; +import tanya.container.array; +import tanya.container.string; /** * Position in the source text. @@ -82,3 +84,23 @@ struct Result(T) return error.isNull; } } + +struct Reference +{ + String name; + size_t offset; +} + +struct Symbol +{ + String name; + Array!ubyte text; + Array!Reference symbols; +} + +struct Relocation +{ + Reference symbol; + ubyte typeInformation; + bool hasEntry = true; +} diff --git a/source/elna/riscv.d b/source/elna/riscv.d index fe6989f..91506a0 100644 --- a/source/elna/riscv.d +++ b/source/elna/riscv.d @@ -2,6 +2,7 @@ module elna.riscv; import elna.extended; import elna.ir; +import elna.result; import std.algorithm; import std.typecons; import tanya.container.array; @@ -115,11 +116,16 @@ struct Instruction { private uint instruction; - ref Instruction i(BaseOpcode opcode, XRegister rd, Funct3 funct3, XRegister rs1, uint immediate) + this(BaseOpcode opcode) @nogc + { + this.instruction = opcode; + } + @disable this(); + + ref Instruction i(XRegister rd, Funct3 funct3, XRegister rs1, uint immediate) return scope @nogc { - this.instruction = opcode - | (rd << 7) + this.instruction |= (rd << 7) | (funct3 << 12) | (rs1 << 15) | (immediate << 20); @@ -127,11 +133,10 @@ struct Instruction return this; } - ref Instruction s(BaseOpcode opcode, uint imm1, Funct3 funct3, XRegister rs1, XRegister rs2, uint imm2 = 0) + ref Instruction s(uint imm1, Funct3 funct3, XRegister rs1, XRegister rs2, uint imm2 = 0) return scope @nogc { - this.instruction = opcode - | (imm1 << 7) + this.instruction |= (imm1 << 7) | (funct3 << 12) | (rs1 << 15) | (rs2 << 20) @@ -140,11 +145,10 @@ struct Instruction return this; } - ref Instruction r(BaseOpcode opcode, XRegister rd, Funct3 funct3, XRegister rs1, XRegister rs2, ubyte funct7 = 0) + ref Instruction r(XRegister rd, Funct3 funct3, XRegister rs1, XRegister rs2, ubyte funct7 = 0) return scope @nogc { - this.instruction = opcode - | (rd << 7) + this.instruction |= (rd << 7) | (funct3 << 12) | (rs1 << 15) | (rs2 << 20) @@ -153,28 +157,51 @@ struct Instruction return this; } + ref Instruction u(XRegister rd, uint imm) + return scope @nogc + { + this.instruction |= (rd << 7) | (imm << 12); + + return this; + } + ubyte[] encode() return scope @nogc { return (cast(ubyte*) (&this.instruction))[0 .. uint.sizeof]; } } -Array!ubyte writeNext(Definition ast) @nogc +Symbol writeNext(Definition ast) @nogc { Array!Instruction instructions; + Array!Reference references; // Prologue. instructions.insertBack( - Instruction() - .i(BaseOpcode.opImm, XRegister.sp, Funct3.addi, XRegister.sp, cast(uint) -16) + Instruction(BaseOpcode.opImm) + .i(XRegister.sp, Funct3.addi, XRegister.sp, cast(uint) -32) ); instructions.insertBack( - Instruction() - .s(BaseOpcode.store, 12, Funct3.sw, XRegister.sp, XRegister.s0) + Instruction(BaseOpcode.store) + .s(28, Funct3.sw, XRegister.sp, XRegister.s0) ); instructions.insertBack( - Instruction() - .i(BaseOpcode.opImm, XRegister.s0, Funct3.addi, XRegister.sp, 16) + Instruction(BaseOpcode.store) + .s(24, Funct3.sw, XRegister.sp, XRegister.ra) + ); + instructions.insertBack( + Instruction(BaseOpcode.opImm) + .i(XRegister.s0, Funct3.addi, XRegister.sp, 32) + ); + + // Code generation. + references.insertBack(Reference(String("putchar"), instructions.length * 4)); + instructions.insertBack( + Instruction(BaseOpcode.auipc).u(XRegister.ra, 0) + ); + instructions.insertBack( + Instruction(BaseOpcode.jalr) + .i(XRegister.ra, Funct3.jalr, XRegister.ra, 0) ); int i = 1; @@ -185,8 +212,8 @@ Array!ubyte writeNext(Definition ast) @nogc // Opcode of mov is “0xb8 + r”, where “r” is the register opcode. // Register opcode of %eax is 0. instructions.insertBack( - Instruction() // movl $x, %eax; where $x is a number. - .i(BaseOpcode.opImm, XRegister.a0, Funct3.addi, XRegister.zero, + Instruction(BaseOpcode.opImm) // movl $x, %eax; where $x is a number. + .i(XRegister.a0, Funct3.addi, XRegister.zero, (cast(Number) statement.subroutine.lhs).value) ); } @@ -194,8 +221,8 @@ Array!ubyte writeNext(Definition ast) @nogc { // movl -x(%rbp), %eax; where x is a number. instructions.insertBack( - Instruction() - .i(BaseOpcode.load, XRegister.a0, Funct3.lw, XRegister.sp, + Instruction(BaseOpcode.load) + .i(XRegister.a0, Funct3.lw, XRegister.sp, cast(byte) (cast(Variable) statement.subroutine.lhs).counter * 4) ); } @@ -204,8 +231,8 @@ Array!ubyte writeNext(Definition ast) @nogc // Opcode of mov is “0xb8 + r”, where “r” is the register opcode. // Register opcode of %ebx is 3. instructions.insertBack( - Instruction() // movl $x, %ebx; where $x is a number. - .i(BaseOpcode.opImm, XRegister.t0, Funct3.addi, XRegister.zero, + Instruction(BaseOpcode.opImm) // movl $x, %ebx; where $x is a number. + .i(XRegister.t0, Funct3.addi, XRegister.zero, (cast(Number) statement.subroutine.rhs).value) ); } @@ -213,42 +240,48 @@ Array!ubyte writeNext(Definition ast) @nogc { // movl -x(%rbp), %ebx; where x is a number. instructions.insertBack( - Instruction() - .i(BaseOpcode.load, XRegister.t0, Funct3.lw, XRegister.sp, + Instruction(BaseOpcode.load) + .i(XRegister.t0, Funct3.lw, XRegister.sp, cast(byte) (cast(Variable) statement.subroutine.rhs).counter * 4) ); } // Calculate the result and assign it to a variable on the stack. instructions.insertBack( - Instruction() - .r(BaseOpcode.op, XRegister.a0, Funct3.add, XRegister.a0, XRegister.t0) + Instruction(BaseOpcode.op) + .r(XRegister.a0, Funct3.add, XRegister.a0, XRegister.t0) ); instructions.insertBack( // movl %eax, -x(%rbp); where x is a number. - Instruction() - .s(BaseOpcode.store, cast(uint) (i * 4), Funct3.sw, XRegister.sp, XRegister.a0) + Instruction(BaseOpcode.store) + .s(cast(uint) (i * 4), Funct3.sw, XRegister.sp, XRegister.a0) ); ++i; } // Prologue. instructions.insertBack( - Instruction() - .i(BaseOpcode.load, XRegister.s0, Funct3.lw, XRegister.sp, 12) + Instruction(BaseOpcode.load) + .i(XRegister.s0, Funct3.lw, XRegister.sp, 28) ); instructions.insertBack( - Instruction() - .i(BaseOpcode.opImm, XRegister.sp, Funct3.addi, XRegister.sp, 16) + Instruction(BaseOpcode.load) + .i(XRegister.ra, Funct3.lw, XRegister.sp, 24) ); instructions.insertBack( - Instruction() - .i(BaseOpcode.jalr, XRegister.zero, Funct3.jalr, XRegister.ra, 0) + Instruction(BaseOpcode.opImm) + .i(XRegister.sp, Funct3.addi, XRegister.sp, 32) + ); + instructions.insertBack( + Instruction(BaseOpcode.jalr) + .i(XRegister.zero, Funct3.jalr, XRegister.ra, 0) ); - Array!ubyte programText; + auto program = Symbol(String("main")); + + program.symbols = move(references); foreach (ref instruction; instructions) { - programText.insertBack(instruction.encode); + program.text.insertBack(instruction.encode); } - return programText; + return program; }