Skip to content

Commit

Permalink
Showing 25 changed files with 337 additions and 344 deletions.
2 changes: 1 addition & 1 deletion src/compiler/crystal/codegen/call.cr
Original file line number Diff line number Diff line change
@@ -344,7 +344,7 @@ class Crystal::CodeGenVisitor
call_arg.set_type(a_def_arg.type)
end
if (node_block = node.block) && node_block.break.type?
call.set_type(@mod.type_merge [a_def.type, node_block.break.type] of Type)
call.set_type(@program.type_merge [a_def.type, node_block.break.type] of Type)
else
call.set_type(a_def.type)
end
6 changes: 3 additions & 3 deletions src/compiler/crystal/codegen/cast.cr
Original file line number Diff line number Diff line change
@@ -376,7 +376,7 @@ class Crystal::CodeGenVisitor

def downcast_distinct(value, to_type : BoolType, from_type : MixedUnionType)
value_ptr = union_value(value)
value = cast_to_pointer(value_ptr, @mod.int8)
value = cast_to_pointer(value_ptr, @program.int8)
value = load(value)
trunc value, LLVM::Int1
end
@@ -564,7 +564,7 @@ class Crystal::CodeGenVisitor
end

def store_bool_in_union(union_type, union_pointer, value)
store type_id(value, @mod.bool), union_type_id(union_pointer)
store type_id(value, @program.bool), union_type_id(union_pointer)

# To store a boolean in a union
# we sign-extend it to the size in bits of the union
@@ -581,7 +581,7 @@ class Crystal::CodeGenVisitor
union_value_type = llvm_union_value_type(target_type)
value = union_value_type.null

store type_id(value, @mod.nil), union_type_id(union_pointer)
store type_id(value, @program.nil), union_type_id(union_pointer)
casted_value_ptr = bit_cast union_value(union_pointer), union_value_type.pointer
store value, casted_value_ptr
end
2 changes: 1 addition & 1 deletion src/compiler/crystal/codegen/class_var.cr
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@ class Crystal::CodeGenVisitor
global = @main_mod.globals.add(llvm_type(type), global_name)
global.linkage = LLVM::Linkage::Internal if @single_module
global.thread_local = true if thread_local
if !global.initializer && type.includes_type?(@mod.nil_type)
if !global.initializer && type.includes_type?(@program.nil_type)
global.initializer = llvm_type(type).null
end
end
84 changes: 42 additions & 42 deletions src/compiler/crystal/codegen/codegen.cr
Original file line number Diff line number Diff line change
@@ -124,21 +124,21 @@ module Crystal
@cant_pass_closure_to_c_exception_call : Call?
@realloc_fun : LLVM::Function?

def initialize(@mod : Program, @node : ASTNode, single_module = false, debug = false, @llvm_mod = LLVM::Module.new("main_module"), expose_crystal_main = true)
def initialize(@program : Program, @node : ASTNode, single_module = false, debug = false, @llvm_mod = LLVM::Module.new("main_module"), expose_crystal_main = true)
@main_mod = @llvm_mod
@single_module = !!single_module
@debug = !!debug
@abi = @mod.target_machine.abi
@llvm_typer = @mod.llvm_typer
@llvm_id = LLVMId.new(@mod)
@abi = @program.target_machine.abi
@llvm_typer = @program.llvm_typer
@llvm_id = LLVMId.new(@program)
@main_ret_type = node.type
ret_type = @llvm_typer.llvm_return_type(node.type)
@main = @llvm_mod.functions.add(MAIN_NAME, [LLVM::Int32, LLVM::VoidPointer.pointer], ret_type)
@main.linkage = LLVM::Linkage::Internal unless expose_crystal_main

emit_main_def_debug_metadata(@main, "??") if @debug

@context = Context.new @main, @mod
@context = Context.new @main, @program
@context.return_type = @main_ret_type

@argc = @main.params[0]
@@ -161,14 +161,14 @@ module Crystal
@strings = {} of StringKey => LLVM::Value
@symbols = {} of String => Int32
@symbol_table_values = [] of LLVM::Value
mod.symbols.each_with_index do |sym, index|
program.symbols.each_with_index do |sym, index|
@symbols[sym] = index
@symbol_table_values << build_string_constant(sym, sym)
end

unless mod.symbols.empty?
unless program.symbols.empty?
symbol_table = define_symbol_table @llvm_mod
symbol_table.initializer = LLVM.array(llvm_type(@mod.string), @symbol_table_values)
symbol_table.initializer = LLVM.array(llvm_type(@program.string), @symbol_table_values)
end

@last = llvm_nil
@@ -197,13 +197,13 @@ module Crystal

initialize_simple_class_vars_and_constants

alloca_vars @mod.vars, @mod
alloca_vars @program.vars, @program
end

# Here we only initialize simple constants and class variables, those
# that has simple values like 1, "foo" and other literals.
def initialize_simple_class_vars_and_constants
@mod.class_var_and_const_initializers.each do |initializer|
@program.class_var_and_const_initializers.each do |initializer|
case initializer
when Const
next unless initializer.simple?
@@ -221,15 +221,15 @@ module Crystal
end

def wrap_builder(builder)
CrystalLLVMBuilder.new builder, @mod.printf(@llvm_mod)
CrystalLLVMBuilder.new builder, @program.printf(@llvm_mod)
end

def define_symbol_table(llvm_mod)
llvm_mod.globals.add llvm_type(@mod.string).array(@symbol_table_values.size), SYMBOL_TABLE_NAME
llvm_mod.globals.add llvm_type(@program.string).array(@symbol_table_values.size), SYMBOL_TABLE_NAME
end

def data_layout
@mod.has_flag?("x86_64") ? DataLayout64 : DataLayout32
@program.has_flag?("x86_64") ? DataLayout64 : DataLayout32
end

class CodegenWellKnownFunctions < Visitor
@@ -280,7 +280,7 @@ module Crystal
end

@unused_fun_defs.each do |node|
codegen_fun node.real_name, node.external, @mod, is_exported_fun: true
codegen_fun node.real_name, node.external, @program, is_exported_fun: true
end

env_dump = ENV["DUMP"]?
@@ -319,14 +319,14 @@ module Crystal

unless node.external.dead
if node.external.used
codegen_fun node.real_name, node.external, @mod, is_exported_fun: true
codegen_fun node.real_name, node.external, @program, is_exported_fun: true
else
# If the fun is not invoked we codegen it at the end so
# we don't have issues with constants being used before
# they are declared.
# But, apparenty, llvm requires us to define them so that
# calls can find them, so we do so.
codegen_fun node.real_name, node.external, @mod, is_exported_fun: false
codegen_fun node.real_name, node.external, @program, is_exported_fun: false
@unused_fun_defs << node
end
end
@@ -336,7 +336,7 @@ module Crystal

def visit(node : FileNode)
with_context(Context.new(context.fun, context.type)) do
file_module = @mod.file_module(node.filename)
file_module = @program.file_module(node.filename)
if vars = file_module.vars?
alloca_vars vars, file_module
end
@@ -456,7 +456,7 @@ module Crystal
# def as returning void. This can't be done in the type inference phase because
# of bindings and type propagation.
if node.force_nil
node.def.set_type @mod.nil
node.def.set_type @program.nil
else
# Use proc literal's type, which might have a broader type then the body
# (for example, return type: Int32 | String, body: String)
@@ -482,7 +482,7 @@ module Crystal
if location && (type = node.type?)
proc_name = true
filename = location.filename.as(String)
fun_literal_name = Crystal.safe_mangling(@mod, "~proc#{type}@#{Crystal.relative_filename(filename)}:#{location.line_number}")
fun_literal_name = Crystal.safe_mangling(@program, "~proc#{type}@#{Crystal.relative_filename(filename)}:#{location.line_number}")
else
proc_name = false
fun_literal_name = "~fun_literal"
@@ -798,10 +798,10 @@ module Crystal
request_value do
accept exp
end
exp.type? || @mod.nil
exp.type? || @program.nil
else
@last = llvm_nil
@mod.nil
@program.nil
end
end

@@ -1108,7 +1108,7 @@ module Crystal

position_at_end doesnt_match_block

temp_var_name = @mod.new_temp_var_name
temp_var_name = @program.new_temp_var_name
context.vars[temp_var_name] = LLVMVar.new(last_value, obj_type, already_loaded: true)
accept type_cast_exception_call(obj_type, to_type, node, temp_var_name)
context.vars.delete temp_var_name
@@ -1146,7 +1146,7 @@ module Crystal
cond cmp, matches_block, doesnt_match_block

position_at_end doesnt_match_block
@last = upcast llvm_nil, resulting_type, @mod.nil
@last = upcast llvm_nil, resulting_type, @program.nil
phi.add @last, resulting_type

position_at_end matches_block
@@ -1174,19 +1174,19 @@ module Crystal

ex = Call.new(Path.global("TypeCastError"), "new", StringInterpolation.new(pieces))
call = Call.global("raise", ex)
call = @mod.normalize(call)
call = @program.normalize(call)

meta_vars = MetaVars.new
meta_vars[var_name] = MetaVar.new(var_name, type: from_type)
visitor = MainVisitor.new(@mod, meta_vars)
@mod.visit_main call, visitor: visitor
visitor = MainVisitor.new(@program, meta_vars)
@program.visit_main call, visitor: visitor
call
end

def cant_pass_closure_to_c_exception_call
@cant_pass_closure_to_c_exception_call ||= begin
call = Call.global("raise", StringLiteral.new("passing a closure to C is not allowed"))
@mod.visit_main call
@program.visit_main call
call
end
end
@@ -1464,7 +1464,7 @@ module Crystal
end

raise_fun = main_fun(RAISE_NAME)
codegen_call_or_invoke(node, nil, nil, raise_fun, [bit_cast(unwind_ex_obj, raise_fun.params.first.type)], true, @mod.no_return)
codegen_call_or_invoke(node, nil, nil, raise_fun, [bit_cast(unwind_ex_obj, raise_fun.params.first.type)], true, @program.no_return)
end

old_last = @last
@@ -1483,7 +1483,7 @@ module Crystal

accept node_ensure
raise_fun = main_fun(RAISE_NAME)
codegen_call_or_invoke(node, nil, nil, raise_fun, [bit_cast(unwind_ex_obj, raise_fun.params.first.type)], true, @mod.no_return)
codegen_call_or_invoke(node, nil, nil, raise_fun, [bit_cast(unwind_ex_obj, raise_fun.params.first.type)], true, @program.no_return)

position_at_end old_block

@@ -1663,10 +1663,10 @@ module Crystal
vars.each do |name, var|
next if name == "self" || context.vars[name]?

var_type = var.type? || @mod.nil
var_type = var.type? || @program.nil

if var_type.void?
context.vars[name] = LLVMVar.new(llvm_nil, @mod.void)
context.vars[name] = LLVMVar.new(llvm_nil, @program.void)
elsif var_type.no_return?
# No alloca for NoReturn
elsif var.closure_in?(obj)
@@ -1681,7 +1681,7 @@ module Crystal

# Assign default nil for variables that are bound to the nil variable
if bound_to_mod_nil?(var)
assign ptr, var_type, @mod.nil, llvm_nil
assign ptr, var_type, @program.nil, llvm_nil
end
else
# The variable belong to an outer closure
@@ -1767,13 +1767,13 @@ module Crystal
vars.each do |name, var|
if var.context == block && bound_to_mod_nil?(var)
context_var = context.vars[name]
assign context_var.pointer, context_var.type, @mod.nil, llvm_nil
assign context_var.pointer, context_var.type, @program.nil, llvm_nil
end
end
end

def bound_to_mod_nil?(var)
var.dependencies.any? &.same?(@mod.nil_var)
var.dependencies.any? &.same?(@program.nil_var)
end

def alloca(type, name = "")
@@ -1815,7 +1815,7 @@ module Crystal
end

def printf(format, args = [] of LLVM::Value)
call @mod.printf(@llvm_mod), [builder.global_string_pointer(format)] + args
call @program.printf(@llvm_mod), [builder.global_string_pointer(format)] + args
end

def allocate_aggregate(type)
@@ -1872,12 +1872,12 @@ module Crystal
value = init.value

# Don't need to initialize false
if ivar.type == @mod.bool && value.false?
if ivar.type == @program.bool && value.false?
next
end

# Don't need to initialize zero
if ivar.type == @mod.int32 && value.zero?
if ivar.type == @program.int32 && value.zero?
next
end

@@ -1929,11 +1929,11 @@ module Crystal

def memset(pointer, value, size)
pointer = cast_to_void_pointer pointer
call @mod.memset(@llvm_mod), [pointer, value, trunc(size, LLVM::Int32), int32(4), int1(0)]
call @program.memset(@llvm_mod), [pointer, value, trunc(size, LLVM::Int32), int32(4), int1(0)]
end

def memcpy(dest, src, len, align, volatile)
call @mod.memcpy(@llvm_mod), [dest, src, len, align, volatile]
call @program.memcpy(@llvm_mod), [dest, src, len, align, volatile]
end

def realloc(buffer, size)
@@ -1943,7 +1943,7 @@ module Crystal
size = trunc(size, LLVM::Int32)
call realloc_fun, [buffer, size]
else
call @mod.realloc(@llvm_mod), [buffer, size]
call @program.realloc(@llvm_mod), [buffer, size]
end
end

@@ -2001,12 +2001,12 @@ module Crystal
global.linkage = LLVM::Linkage::Private
global.global_constant = true
global.initializer = LLVM.struct [
type_id(@mod.string),
type_id(@program.string),
int32(str.bytesize),
int32(str.size),
LLVM.string(str),
]
cast_to global, @mod.string
cast_to global, @program.string
end
end

4 changes: 2 additions & 2 deletions src/compiler/crystal/codegen/cond.cr
Original file line number Diff line number Diff line change
@@ -39,13 +39,13 @@ class Crystal::CodeGenVisitor
type_id = load union_type_id(@last)

if has_nil
is_nil = equal? type_id, type_id(@mod.nil)
is_nil = equal? type_id, type_id(@program.nil)
cond = and cond, not(is_nil)
end

if has_bool
value = load(bit_cast union_value(@last), LLVM::Int1.pointer)
is_bool = equal? type_id, type_id(@mod.bool)
is_bool = equal? type_id, type_id(@program.bool)
cond = and cond, not(and(is_bool, not(value)))
end

4 changes: 2 additions & 2 deletions src/compiler/crystal/codegen/const.cr
Original file line number Diff line number Diff line change
@@ -34,7 +34,7 @@ class Crystal::CodeGenVisitor
# in the main function
def initialize_argv_and_argc
{"ARGC_UNSAFE", "ARGV_UNSAFE"}.each do |name|
const = @mod.types[name].as(Const)
const = @program.types[name].as(Const)
global = declare_const(const)
request_value do
accept const.value
@@ -162,7 +162,7 @@ class Crystal::CodeGenVisitor
end

def read_const_pointer(const)
if const == @mod.argc || const == @mod.argv
if const == @program.argc || const == @program.argv
global_name = const.llvm_name
global = declare_const(const)

Loading

0 comments on commit fecbc1c

Please sign in to comment.