Skip to content

Commit

Permalink
change compile method to to_elf method
Browse files Browse the repository at this point in the history
  • Loading branch information
katsyoshi committed May 20, 2023
1 parent 583efbf commit 97983b8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/vaporware/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def compile(compiler: "gcc", compiler_options: ["-O0"])
end
output.close
compiler_options += @generator.compile_shared_option if @generator.shared
@generator.call_compiler(compiler:, compiler_options:)
@generator.to_elf(input: @generator.precompile, compiler:, compiler_options:, debug: @generator.debug)
end
end
end
16 changes: 11 additions & 5 deletions lib/vaporware/compiler/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,22 @@ def register_var_and_method(node)
def already_build_methods? = defined_methods.sort == @doned.to_a.sort
def variable_or_method?(type) = [:lvasgn, :arg, :def].include?(type)

def call_compiler(output: precompile, compiler: "gcc", compiler_options: ["-O0"], debug: false)
base_name = File.basename(output, ".*")
def to_elf(input: precompile, compiler: "gcc", compiler_options: ["-O0"], debug: false)
base_name = File.basename(input, ".*")
name = shared ? "lib#{base_name}.so" : base_name
compile_commands = [compiler, *compiler_options, "-o", name, output].compact
IO.popen(compile_commands).close
if compiler.nil?
Vaporware::Compiler::Assemble.compile!(name, input)
else
compile_commands = [compiler, *compiler_options, "-o", name, input].compact
call_compiler(compile_commands)
end

File.delete(output) unless debug
File.delete(input) unless debug
nil
end

def call_compiler(compile_commands) = IO.popen(compile_commands).close

def epilogue(output)
output.puts " mov rsp, rbp"
output.puts " pop rbp"
Expand Down
3 changes: 2 additions & 1 deletion sig/vaporware.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ module Vaporware
def already_build_methods?: -> bool
def args: (untyped, File) -> nil
def build: (untyped, File, ?bool) -> nil
def call_compiler: (?output: String, ?compiler: String, ?compiler_options: Array[String], ?debug: bool) -> nil
def call_compiler: (Array[String]) -> nil
def call_method: (untyped, File, bool) -> nil
def comp: (String, File) -> nil
def compile_shared_option: () -> Array[String]
Expand All @@ -52,6 +52,7 @@ module Vaporware
def ret: (File) -> nil
def lvar_offset: (Symbol | nil) -> Integer
def register_var_and_method: (untyped) -> nil
def to_elf: (?input: String, ?compiler: String, ?compiler_options: Array[String], ?debug: bool) -> nil
def variable_or_method?: (Symbol) -> bool
end
end
Expand Down

0 comments on commit 97983b8

Please sign in to comment.