-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathcontext.cr
465 lines (364 loc) · 14.6 KB
/
context.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
require "./repl"
# This class contains all of the global data used to interpret a single
# program. For example, it includes the memory region to store constants
# and class variables, what are all the know symbols, and a few more things.
class Crystal::Repl::Context
record MultidispatchKey, obj_type : Type, call_signature : CallSignature
getter program : Program
# A hash of Def to their compiled representation, so we don't compile
# a single method multiple times.
# The exceptions are methods with non-captured blocks.
getter defs : Hash(Def, CompiledDef)
# Information about all known constants.
getter! constants : Constants
# Information about all known class variables.
getter! class_vars : ClassVars
# libffi information about external functions.
getter lib_functions : Hash(External, LibFunction)
# Cache of multidispatch expansions.
getter multidispatchs : Hash(MultidispatchKey, Def)
# The single closure function that we use for all function pointers (procs)
# passed to C. This closure function knows all the information about
# the callback being passed, and is able to run the code associated to it.
getter ffi_closure_fun : LibFFI::ClosureFun
# The memory where constants are stored. Refer to `Constants` for more on this.
property constants_memory : Pointer(UInt8)
# The memory where class vars are stored. Refer to `ClassVars` for more on this.
property class_vars_memory : Pointer(UInt8)
# Associated an FFI::Closure's code to a CompiledDef.
# When we set an extern struct's field that is a Proc, we create
# an FFI::Closure object and set that instead of a Proc.
# In case a user reads that field back we need to create a Proc,
# we can't use an FFI::Closure object. In that case we lookup
# the proc in this Hash.
getter ffi_closure_to_compiled_def : Hash(Void*, CompiledDef)
def initialize(@program : Program)
@program.flags << "interpreted"
@gc_references = [] of Void*
@defs = {} of Def => CompiledDef
@defs.compare_by_identity
@lib_functions = {} of External => LibFunction
@lib_functions.compare_by_identity
@symbol_to_index = {} of String => Int32
@symbols = [] of String
@multidispatchs = {} of MultidispatchKey => Def
@ffi_closure_contexts = {} of {UInt64, UInt64} => FFIClosureContext
@constants_memory = Pointer(Void).malloc(1).as(UInt8*)
@class_vars_memory = Pointer(Void).malloc(1).as(UInt8*)
@ffi_closure_to_compiled_def = {} of Void* => CompiledDef
@type_instance_var_initializers = {} of Type => Array(CompiledDef)
@ffi_closure_fun = LibFFI::ClosureFun.new do |cif, ret, args, user_data|
Context.ffi_closure_fun(cif, ret, args, user_data)
nil
end
# This is a stack pool, for checkout_stack.
@stack_pool = Fiber::StackPool.new(protect: false)
# Mapping of types to numeric ids
@type_to_id = {} of Type => Int32
@id_to_type = [] of Type
@constants = Constants.new(self)
@class_vars = ClassVars.new(self)
# Nil has type id 0, String has type id 1
type_id(@program.nil_type)
type_id(@program.string)
end
getter(throw_value_type : Type) do
@program.static_array_of(@program.uint8, sizeof(Interpreter::ThrowValue))
end
# Many reference values we create when compiling nodes to bytecode
# must not be collected by the GC. Ideally they should be referenced
# in the bytecode itself. The problem is that the bytecode isn't
# always aligned to 8 bytes boundaries. So until we figure out what's
# the proper way to do it, we just retain these references here.
def add_gc_reference(ref : Reference)
@gc_references << ref.as(Void*)
end
# Checks out a stack from the stack pool and yields it to the given block.
# Once the block returns, the stack is returned to the pool.
# The stack is not cleared after or before it's used.
def checkout_stack(& : UInt8* -> _)
stack, _ = @stack_pool.checkout
begin
yield stack.as(UInt8*)
ensure
@stack_pool.release(stack)
end
end
# This returns the CompiledDef that corresponds to __crystal_raise_overflow
getter(crystal_raise_overflow_compiled_def : CompiledDef) do
call = Call.new(nil, "__crystal_raise_overflow", global: true)
program.semantic(call)
local_vars = LocalVars.new(self)
compiler = Compiler.new(self, local_vars)
compiler.compile(call)
defs[call.target_def]
end
record InstanceVarInitializer,
initializer : InstanceVarInitializerContainer::InstanceVarInitializer,
owner : Type
def type_instance_var_initializers(type : Type)
@type_instance_var_initializers[type] ||= begin
initializers = [] of InstanceVarInitializer
collect_instance_vars_initializers(type, initializers)
initializers.map do |initializer|
a_def = create_instance_var_initializer_def(type, initializer)
compiled_def = CompiledDef.new(self, a_def, a_def.owner, sizeof(Pointer(Void)))
compiled_def.local_vars.declare("self", type)
initializer.initializer.meta_vars.each do |name, var|
var_type = var.type?
next unless var_type
compiled_def.local_vars.declare(name, var_type)
end
compiler = Compiler.new(self, compiled_def, top_level: false)
compiler.compile_def(compiled_def)
{% if Debug::DECOMPILE %}
puts "=== #{a_def.name} ==="
puts Disassembler.disassemble(self, compiled_def)
puts "=== #{a_def.name} ==="
{% end %}
compiled_def
end
end
end
private def collect_instance_vars_initializers(type : ClassType | GenericClassInstanceType, collected) : Nil
if superclass = type.superclass
collect_instance_vars_initializers superclass, collected
end
collect_instance_vars_initializers_non_recursive type, collected
end
private def collect_instance_vars_initializers(type : Type, collected) : Nil
# Nothing to do
end
private def collect_instance_vars_initializers_non_recursive(type : Type, collected) : Nil
initializers = type.instance_vars_initializers
initializers.try &.each do |initializer|
collected << InstanceVarInitializer.new(initializer, type)
end
end
private def create_instance_var_initializer_def(type : Type, initializer : InstanceVarInitializer)
# Creates a def that will assign the initializer's value to the instance variable.
# The initializer's value is fully typed already, so we don't need to type it
# again. We can just create the assignment and type those nodes for the
# interpreter compiler to be able to compile it.
value = initializer.initializer.value
ivar = InstanceVar.new(initializer.initializer.name)
ivar.type = value.type
assign = Assign.new(ivar, value)
assign.type = value.type
a_def = Def.new("initialize_#{initializer.initializer.name}", args: [Arg.new("self", type: type)])
a_def.body = assign
a_def.type = program.nil_type
a_def.owner = type
vars = initializer.initializer.meta_vars.clone
vars["self"] = MetaVar.new("self", type)
a_def.vars = vars
a_def
end
def symbol_index(symbol : String) : Int32
# TODO: use a string pool?
index = @symbol_to_index[symbol]?
unless index
index = @symbol_to_index.size
@symbol_to_index[symbol] = index
@symbols << symbol
end
index
end
def index_to_symbol(index : Int32) : String
@symbols[index]
end
def declare_const(const : Const, compiled_def : CompiledDef) : Int32
constants.declare(const, compiled_def)
end
def const_index_and_compiled_def?(const : Const) : {Int32, CompiledDef}?
value = constants.fetch?(const)
if value
{value.index, value.compiled_def}
else
nil
end
end
def declare_class_var(owner : Type, name : String, type : Type, compiled_def : CompiledDef?) : Int32
class_vars.declare(owner, name, type, compiled_def)
end
def class_var_index_and_compiled_def(owner : Type, name : String) : {Int32, CompiledDef?}?
value = class_vars.fetch?(owner, name)
if value
{value.index, value.compiled_def}
else
nil
end
end
def ffi_closure_context(interpreter : Interpreter, compiled_def : CompiledDef)
# Keep the closure contexts in a Hash by the compiled def so we don't
# lose a reference to it in the GC.
@ffi_closure_contexts[{interpreter.object_id, compiled_def.object_id}] ||= FFIClosureContext.new(interpreter, compiled_def)
end
protected def self.ffi_closure_fun(cif : LibFFI::Cif*, ret : Void*, args : Void**, user_data : Void*)
# This is the generic callback that gets called on any C callback.
closure_context = user_data.as(FFIClosureContext)
interpreter = closure_context.interpreter
compiled_def = closure_context.compiled_def
# What to do:
# - create a new interpreter that uses the same stack
# (call the second initialize overload)
# - copy args into the stack, starting from stack_top
# - call interpret on the compiled_def.def.body
# - copy the value back to ret
interpreter.context.checkout_stack do |stack|
stack_top = stack
# Clear the proc's local vars area, the stack might have garbage there
stack_top.clear(compiled_def.local_vars.max_bytesize)
compiled_def.def.args.each_with_index do |arg, i|
args[i].as(UInt8*).copy_to(stack_top, interpreter.inner_sizeof_type(arg.type))
stack_top += interpreter.aligned_sizeof_type(arg.type)
end
# TODO: maybe we don't need a new interpreter for this?
sub_interpreter = Interpreter.new(interpreter, compiled_def, stack, 0)
value = sub_interpreter.interpret(compiled_def.def.body, compiled_def.def.vars.not_nil!)
value.copy_to(ret.as(UInt8*))
end
end
def aligned_sizeof_type(node : ASTNode) : Int32
aligned_sizeof_type(node.type?)
end
def aligned_sizeof_type(type : Type) : Int32
align(inner_sizeof_type(type))
end
def aligned_sizeof_type(type : Nil) : Int32
0
end
def inner_sizeof_type(node : ASTNode) : Int32
inner_sizeof_type(node.type?)
end
def inner_sizeof_type(type : Type) : Int32
@program.size_of(type.sizeof_type).to_i32
end
def inner_sizeof_type(type : Nil) : Int32
0
end
def inner_alignof_type(node : ASTNode) : Int32
inner_alignof_type(node.type?)
end
def inner_alignof_type(type : Type) : Int32
@program.align_of(type.sizeof_type).to_i32
end
def inner_alignof_type(type : Nil) : Int32
0
end
def aligned_instance_sizeof_type(type : Type) : Int32
align(inner_instance_sizeof_type(type))
end
def inner_instance_sizeof_type(node : ASTNode) : Int32
inner_instance_sizeof_type(node.type?)
end
def inner_instance_sizeof_type(type : Type) : Int32
@program.instance_size_of(type.sizeof_type).to_i32
end
def inner_instance_sizeof_type(type : Nil) : Int32
0
end
def inner_instance_alignof_type(node : ASTNode) : Int32
inner_instance_alignof_type(node.type?)
end
def inner_instance_alignof_type(type : Type) : Int32
@program.instance_align_of(type.sizeof_type).to_i32
end
def inner_instance_alignof_type(type : Nil) : Int32
0
end
def offset_of(type : Type, index : Int32) : Int32
@program.offset_of(type.sizeof_type, index).to_i32
end
def instance_offset_of(type : Type, index : Int32) : Int32
@program.instance_offset_of(type.sizeof_type, index).to_i32
end
def ivar_offset(type : Type, name : String) : Int32
ivar_index = type.index_of_instance_var(name).not_nil!
if type.is_a?(VirtualType) && type.struct? && type.abstract?
# If the type is a virtual abstract struct then the type
# is actually represented as {type_id, value} so the offset
# of the instance var is behind type_id, which is 8 bytes
@program.offset_of(type.base_type, ivar_index).to_i32 + 8
elsif type.passed_by_value?
@program.offset_of(type.sizeof_type, ivar_index).to_i32
else
@program.instance_offset_of(type.sizeof_type, ivar_index).to_i32
end
end
def type_id(type : Type) : Int32
id = @type_to_id[type]?
unless id
id = @id_to_type.size
@id_to_type << type
@type_to_id[type] = id
end
id
end
def type_from_id(id : Int32) : Type
@id_to_type[id]
end
getter? loader : Loader?
getter(loader : Loader) {
lib_flags = program.lib_flags
# Execute and expand `subcommands`.
lib_flags = lib_flags.gsub(/`(.*?)`/) { `#{$1}`.chomp }
args = Process.parse_arguments(lib_flags)
# FIXME: Part 1: This is a workaround for initial integration of the interpreter:
# The loader can't handle the static libgc.a usually shipped with crystal and loading as a shared library conflicts
# with the compiler's own GC.
# (Windows doesn't seem to have this issue)
unless program.has_flag?("win32") && program.has_flag?("gnu")
args.delete("-lgc")
end
# recreate the MSVC developer prompt environment, similar to how compiled
# code does it in `Compiler#linker_command`
if program.has_flag?("msvc")
_, link_args = program.msvc_compiler_and_flags
args.concat(link_args)
end
Crystal::Loader.parse(args, dll_search_paths: dll_search_paths).tap do |loader|
# FIXME: Part 2: This is a workaround for initial integration of the interpreter:
# We append a handle to the current executable (i.e. the compiler program)
# to the loader's handle list. This gives the loader access to all the symbols in the compiler program,
# including those from statically linked libraries like libgc.
# This probably won't work for a fully statically linked compiler.
# But `Crystal::Loader` currently doesn't support that anyways.
loader.load_current_program_handle
if ENV["CRYSTAL_INTERPRETER_LOADER_INFO"]?.presence
STDERR.puts "Crystal::Loader loaded libraries:"
loader.loaded_libraries.each do |path|
STDERR.puts " #{path}"
end
end
end
}
# Extra DLL search paths to mimic compiled code's DLL-copying behavior
# regarding `@[Link]` annotations. These directories should match the ones
# used in `Crystal::Program#each_dll_path`
private def dll_search_paths
{% if flag?(:msvc) %}
paths = CrystalLibraryPath.default_paths
if executable_path = Process.executable_path
paths << File.dirname(executable_path)
end
ENV["PATH"]?.try &.split(Process::PATH_DELIMITER, remove_empty: true) do |path|
paths << path
end
paths
{% else %}
nil
{% end %}
end
def c_function(name : String)
loader.find_symbol(name)
end
def align(size : Int32) : Int32
rem = size.remainder(8)
if rem == 0
size
else
size + (8 - rem)
end
end
end