-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
138 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,116 @@ | ||
# frozen_string_literal: true | ||
|
||
module Vaporware | ||
class Compiler::Assemble | ||
attr_reader :input, :output | ||
def self.compile!(input, output = File.basename(input, ".*")) = new(input, output).compile | ||
class Compiler | ||
class Assemble | ||
EXEC_ELF_HEADER = %w( | ||
7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 | ||
02 00 3e 00 01 00 00 00 78 00 40 00 00 00 00 00 | ||
40 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ||
00 00 00 00 40 00 38 00 01 00 40 00 00 00 00 00 | ||
).map { _1.to_i(16) }.pack("C*") | ||
|
||
def initialize(input, output = File.basename(input, ".*")) | ||
@input, @output = input, output | ||
@target_file = File.open(output, "wb") | ||
end | ||
SHARED_ELF_HEADER = %w( | ||
7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 | ||
03 00 3e 00 01 00 00 00 78 00 40 00 00 00 00 00 | ||
40 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 | ||
00 00 00 00 40 00 38 00 01 00 40 00 00 00 00 00 | ||
).map { _1.to_i(16) }.pack("C*") | ||
|
||
PROGRAM_ELF_HEADER = %w( | ||
01 00 00 00 07 00 00 00 00 00 00 00 00 00 00 00 | ||
00 00 40 00 00 00 00 00 00 00 40 00 00 00 00 00 | ||
90 00 00 00 00 00 00 00 90 00 00 00 00 00 00 00 | ||
00 00 20 00 00 00 00 00 | ||
).map { _1.to_i(16) }.pack("C*") | ||
|
||
PREFIX = { | ||
REX_W: "48", | ||
} | ||
|
||
REGISTER_CODE = { | ||
RAX: 0, | ||
RDI: 7, | ||
} | ||
|
||
attr_reader :input, :output | ||
def self.assemble!(input, output = File.basename(input, ".*")) = new(input, output).assemble | ||
|
||
def initialize(input, output = File.basename(input, ".*") + ".o") | ||
@input, @output = input, output | ||
@target_file = File.open(output, "wb") | ||
end | ||
|
||
def assemble(assemble_command: "as", assemble_options: [], input: @input, f: @target_file) | ||
f.write(EXEC_ELF_HEADER) | ||
f.write(PROGRAM_ELF_HEADER) | ||
read = { main: false } | ||
File.open(input, "r") do |r| | ||
r.each_line do |line| | ||
read[:main] = /main:/.match(line) unless read[:main] | ||
next unless read[:main] && !/main:/.match(line) | ||
op, *args = line.split(/\s+/).reject{ _1 == "" }.map { _1.gsub(/,/, "") } | ||
p [op, args] | ||
opes = opecode(op, args) | ||
f.write(opes.pack("C*")) | ||
end | ||
end | ||
f.close | ||
f.path | ||
end | ||
|
||
private | ||
|
||
def opecode(op, args) | ||
case op | ||
when "push" | ||
push(args).map { _1.to_i(16) } | ||
when "mov", "sub", "add" | ||
[PREFIX[:REX_W], *mov_sub(op, *args)].map{ _1.to_i(16) } | ||
when "pop" | ||
pop(args) | ||
end | ||
end | ||
|
||
def mov_sub(*argments) | ||
_op, *args = argments | ||
args.map { reg(_1) } | ||
end | ||
|
||
def push(args) | ||
case args | ||
in ["rbp"] | ["rdi"] | ||
[0x50 + 5].map { _1.to_s(16) } | ||
else | ||
["6a", *args] | ||
end | ||
end | ||
|
||
def pop(args) | ||
case args | ||
in ["rax"] | ["rdi"] | ||
[0x58 + REGISTER_CODE[args.first.upcase.to_sym]] | ||
end | ||
end | ||
|
||
def compile(compile_options = []) | ||
self | ||
def reg(r) | ||
case r | ||
in "rsp" | ||
"89" | ||
in "rbp" | ||
"5e" | ||
in "rax" | ||
"29" | ||
in "rdi" | ||
"f8" | ||
in /\d+/ | ||
r | ||
in "mov" | ||
"89" | ||
in "sub" | ||
"83" | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class Vaporware::Compiler::Assemble | ||
attr_reader input: String | ||
attr_reader output: String | ||
|
||
@target_file: File | ||
|
||
def initialize: (String, String) -> void | ||
def compile: (?compiler_options: Array[String]) -> void | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters