-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathcompiler.rb
835 lines (715 loc) · 25.5 KB
/
compiler.rb
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
#!/bin/env ruby
require 'set'
$: << File.dirname(__FILE__)
require 'emitter'
require 'parser'
require 'scope'
require 'function'
require 'extensions'
require 'ast'
require 'transform'
require 'print_sexp'
require 'compile_arithmetic'
require 'compile_comparisons'
require 'compile_calls'
require 'compile_class'
require 'compile_control'
require 'compile_include'
require 'compile_pragma'
require 'trace'
require 'stackfence'
require 'saveregs'
require 'splat'
require 'value'
require 'output_functions'
require 'globals'
require 'debugscope'
class Compiler
attr_reader :global_functions, :global_scope
attr_writer :trace, :stackfence
# list of all predefined keywords with a corresponding compile-method
# call & callm are ignored, since their compile-methods require
# a special calling convention
@@keywords = Set[
:do, :class, :defun, :defm, :if, :lambda,
:assign, :while, :index, :bindex, :let, :case, :ternif,
:hash, :return,:sexp, :module, :rescue, :incr, :decr, :block,
:required, :add, :sub, :mul, :div, :shl, :sar, :eq, :ne,
:lt, :le, :gt, :ge,:saveregs, :and, :or,
:preturn, :proc, :stackframe, :deref, :include,
:protected, :array, :splat, :mod, :or_assign, :break, :next,
:__compiler_internal, # See `compile_pragma.rb`
:__inline # See `inline.rb`
]
Keywords = @@keywords
@@oper_methods = Set[ :<< ]
def initialize emitter = Emitter.new
@e = emitter
@global_functions = Globals.new
@string_constants = {}
@global_constants = Set.new
@global_constants << :false
@global_constants << :true
@global_constants << :nil
@classes = {}
@vtableoffsets = VTableOffsets.new
@trace = false
# FIXME: Added as workaround as compiler does not auto-initialize
# member variables (yuck)
@global_scope = nil
@lastpos = nil
@linelabel = 0
@section = 0
end
# Outputs nice compiler error messages, similar to
# the parser (ParserBase#error).
def error(error_message, current_scope = nil, current_exp = nil)
if current_exp.respond_to?(:position) && current_exp.position && current_exp.position.lineno
pos = current_exp.position
location = " @ #{pos.inspect}"
elsif @lastpos
location = " near (after) #{@lastpos}"
else
location = ""
end
raise "Compiler error: #{error_message}#{location}\n
current expression: #{current_exp.inspect}\n"
end
# Prints out a warning to the console.
# Similar to error, but doesn't throw an exception, only prints out a message
# and any given additional arguments during compilation process to the console.
def warning(warning_message, *args)
STDERR.puts("#{warning_message} - #{args.join(',')}")
end
# Allocate a symbol
def intern(scope,sym)
# FIXME: Do this once, and add an :assign to a global var, and use that for any
# later static occurrences of symbols.
Value.new(get_arg(scope,[:sexp,[:call,:__get_symbol, sym.to_s]]),:object)
end
# For our limited typing we will in some cases need to do proper lookup.
# For now, we just want to make %s(index __env__ xxx) mostly treated as
# objects, in order to ensure that variables accesses that gets rewritten
# to indirect via __env__ gets treated as object. The exception is
# for now __env__[0] which contains a stackframe pointer used by
# :preturn.
def lookup_type(var, index = nil)
(var == :__env__ && index != 0) ? :object : nil
end
# Returns an argument with its type identifier.
#
# If an Array is given, we have a subexpression, which needs to be compiled first.
# If a Fixnum is given, it's an int -> [:int, a]
# If it's a Symbol, its a variable identifier and needs to be looked up within the given scope.
# Otherwise, we assume it's a string constant and treat it like one.
def get_arg(scope, a, save = false)
return compile_exp(scope, a) if a.is_a?(Array)
return get_arg(scope,:true, save) if a == true
return get_arg(scope,:false, save) if a == false
return Value.new([:int, a]) if (a.is_a?(Fixnum))
return Value.new([:int, a.to_i]) if (a.is_a?(Float)) # FIXME: uh. yes. This is a temporary hack
if a == :"block_given?"
return compile_exp(scope,
[:if,
[:ne, :__closure__, 0],
:true, :false])
end
if a == :"caller"
return compile_exp(scope,
[:sexp, [:__get_string, "FIXME: caller not implemented yet"]])
end
arg = nil
if (a.is_a?(Symbol))
name = a.to_s
return intern(scope,name[1..-1]) if name[0] == ?:
arg = scope.get_arg(a)
# If this is a local variable or argument, we either
# obtain the argument it is cached in, or we cache it
# if possible. If we are calling #get_arg to get
# a target to *save* a value to (assignment), we need
# to mark it as dirty to ensure we save it back to memory
# (spill it) if we need to evict the value from the
# register to use it for something else.
if arg.first == :lvar || arg.first == :arg || (arg.first == :global && arg.last == :self)
reg = @e.cache_reg!(name, arg.first, arg.last, save)
# FIXME: Need to check type
return Value.new([:reg,reg],:object) if reg
end
# FIXME: Check type
return Value.new(arg, :object)
end
warning("nil received by get_arg") if !a
return strconst(a)
end
def strconst(a)
lab = @string_constants[a]
if !lab # For any constants in s-expressions
lab = @e.get_local
@string_constants[a] = lab
end
return Value.new([:addr,lab])
end
# Outputs all constants used within the code generated so far.
# Outputs them as string and global constants, respectively.
def output_constants
@e.rodata { @string_constants.each { |c, l| @e.string(l, c) } }
# FIXME: Temporary workaround to add bss entries for "missing" globals
vars = (@global_constants.to_a + @global_scope.globals.keys).collect{|s| s.to_s}.sort.uniq - ["__roots_start","__roots_end"]
@e.bss do
#@e.bsslong("__stack_top")
@e.label("__roots_start")
vars.each { |c| @e.bsslong(c) }
@e.label("__roots_end")
end
end
# Need to clean up the name to be able to use it in the assembler.
# Strictly speaking we don't *need* to use a sensible name at all,
# but it makes me a lot happier when debugging the asm.
def clean_method_name(name)
dict = {
"?" => "__Q", "!" => "__X",
"[]" => "__NDX", "==" => "__eq",
">=" => "__ge", "<=" => "__le",
"<" => "__lt", ">" => "__gt",
"/" => "__div", "*" => "__mul",
"+" => "__plus", "-" => "__minus"}
pos = 0
# FIXME: this is necessary because we
# currently don't define Symbol#[]
name = name.to_s
len = name.length
out = ""
while (pos < len)
c = name[pos].chr
co = c.ord
pos += 1
if (co >= ?a.ord &&
co <= ?z.ord) ||
(co >= ?A.ord &&
co <= ?Z.ord) ||
(co >= ?0.ord &&
co <= ?9.ord) ||
co == ?_.ord
out << c
else
cn = name[pos]
if cn
ct = c + cn.chr
else
ct = nil
end
if dict[ct]
out << dict[ct]
pos += 1
elsif dict[c]
out << dict[c]
else
out << "__#{co.to_s(16)}"
end
end
end
out
end
# Handle e.g. Tokens::Atom, which is parsed as (deref Tokens Atom)
#
# For now we are assuming statically resolvable chains, and not
# tested multi-level dereference (e.g. Foo::Bar::Baz)
#
def compile_deref(scope, left, right)
cscope = scope.find_constant(left)
if !cscope || !cscope.is_a?(ModuleScope)
error("Unable to resolve: #{left}::#{right} statically (FIXME)",scope)
end
get_arg(cscope,right)
end
# Compiles a function definition.
# Takes the current scope, in which the function is defined,
# the name of the function, its arguments as well as the body-expression that holds
# the actual code for the function's body.
#
# Note that compile_defun is now only accessed via s-expressions
def compile_defun(scope, name, args, body, break_label = nil)
raise "Internal error: Expecting a name; got #{name.inspect}" if name.is_a?(Array)
f = Function.new(name,args, body, scope, break_label || @e.get_local, false)
name = clean_method_name(name)
# add function to the global list of functions defined so far
name = @global_functions.set(name,f)
# a function is referenced by its name (in assembly this is a label).
# wherever we encounter that name, we really need the adress of the label.
# so we mark the function with an adress type.
return Value.new([:addr, clean_method_name(name)])
end
def compile_rescue(scope, rval, lval)
warning("RESCUE is NOT IMPLEMENTED")
compile_exp(scope,lval)
end
def compile_decr(scope, left, right)
compile_assign(scope, left, [:callm, left, :-, [right]])
end
def compile_incr(scope, left, right)
compile_assign(scope, left, [:callm, left, :+, [right]])
end
# Shortcircuit 'left && right' is equivalent to 'if left; right; end'
def compile_and scope, left, right
compile_if(scope, left, right)
end
def combine_types(left, right)
type = nil
if left
if (!right || left.type == right.type)
type = left.type
end
end
return Value.new([:subexpr],type)
end
# Compiles the ternary if form (cond ? then : else)
# It may be better to transform this into the normal
# if form in the tree.
def compile_ternif(scope, cond, alt)
if alt.is_a?(Array) && alt[0] == :ternalt
if_arm = alt[1]
else_arm = alt[2]
else
if_arm = alt
end
compile_if(scope,cond,if_arm,else_arm)
end
def compile_hash(scope, *args)
pairs = []
args.collect do |pair|
if !pair.is_a?(Array) || pair[0] != :pair
error("Literal Hash must contain key value pairs only",scope,args)
end
pairs << pair[1]
pairs << pair[2]
end
compile_callm(scope, :Hash, :[], pairs)
end
# FIXME: Compiler @bug: This method was a self-recursive
# lambda in `#compile_case`
def compile_case_test(compare_exp, test_exprs)
test_value = test_exprs
xrest = []
if test_exprs.is_a?(Array)
#STDERR.puts test_exprs.inspect
if test_exprs[0] == :comma
test_value = test_exprs[1]
xrest = Array(test_exprs[2])
end
#STDERR.puts xrest.inspect
end
cmp = [:callm, test_value, :===, [compare_exp]]
if xrest.empty?
cmp
else
[:or, cmp, compile_case_test(compare_exp, xrest)]
end
end
# FIXME: This is unsafe. It only works for the compiler
# for now because none of the case expressions in the
# compiler itself have side effects.
def compile_whens(compare_exp, whens)
exp = whens.first
if exp[0] == :when
test_values = exp[1]
body = exp[2] # body to be executed, if compare_exp === test_value
@e.comment("test_value: #{test_values.inspect}")
@e.comment("body: #{body.inspect}")
xrest = whens.slice(1..-1)
if xrest.empty?
xrest = [:do]
else
xrest = compile_whens(compare_exp, xrest)
end
[:do, [:if, compile_case_test(compare_exp, test_values), [:do]+body, xrest]]
else
[:do]+exp
end
end
def compile_case(scope, *args)
# FIXME: Compiler @bug:
# The `xrest`'s below were `rest` but that causes `rest` in the
# expression `arg.rest` to be misinterpreted during rewrite to
# method call relative to the contents of the `rest` variable,
# which needless to say is a total disaster.
#
# Further, there is likely another problem here, in that it looks like
# a single, shared, environment is created for the two lambdas, but that
# may be unavoidable given Ruby semantics.
# FIXME:
# Implement like this: compile_eval_arg
# save the register, and loop over the "when"'s.
# Compile each of the "when"'s as "if"'s where the value
# is loaded from the stack and compared with the value
# (or values) in the when clause
@e.comment("compiling case expression")
compare_exp = args.first
@e.comment("compare_exp: #{compare_exp}")
xrest = args.rest
exprs = xrest[0]
if xrest[1]
exprs << xrest[1]
end
exprs = compile_whens(compare_exp, exprs)
compile_eval_arg(scope, exprs)
return Value.new([:subexpr])
end
def compile_stackframe(scope)
@e.comment("Stack frame")
Value.new([:reg,:ebp])
end
# "Special" return for `proc` and bare blocks
# to exit past Proc#call.
def compile_preturn(scope, arg = nil)
@e.comment("preturn")
@e.save_result(compile_eval_arg(scope, arg)) if arg
@e.pushl(:eax)
# We load the return address pre-saved in __stackframe__ on creation of the proc.
# __stackframe__ is automatically added to __env__ in `rewrite_let_env`
ret = compile_eval_arg(scope,[:index,:__env__,0])
@e.movl(ret,:ebp)
@e.movl("-4(%ebp)",:ebx) # Restoring numargs from outside scope
@e.popl(:eax)
@e.leave
@e.ret
@e.evict_all
return Value.new([:subexpr])
end
# Compiles and evaluates a given argument within a given scope.
def compile_eval_arg(scope, arg)
if arg.respond_to?(:position) && arg.position != nil
pos = arg.position.inspect
if pos != @lastpos
if arg[0] != :defm
@e.lineno(arg.position)
end
# trace(arg.position,arg)
end
@lastpos = pos
end
args = get_arg(scope,arg)
error("Unable to find '#{arg.inspect}'") if !args
atype = args[0]
aparam = args[1]
if atype == :ivar
ret = compile_eval_arg(scope, :self)
@e.load_instance_var(ret, aparam)
# FIXME: Verify type of ivar
return Value.new(@e.result_value, :object)
elsif atype == :possible_callm
return Value.new(compile_eval_arg(scope,[:callm,:self,aparam,[]]), :object)
end
return Value.new(@e.load(atype, aparam), args.type)
end
# Compiles an assignment statement.
def compile_assign(scope, left, right)
# transform "foo.bar = baz" into "foo.bar=(baz) - FIXME: Is this better handled in treeoutput.rb?
# Also need to handle :call equivalently.
if left.is_a?(Array) && left[0] == :callm && left.size == 3 # no arguments
return compile_callm(scope, left[1], (left[2].to_s + "=").to_sym, right)
end
source = compile_eval_arg(scope, right)
atype = nil
aparam = nil
@e.pushl(source) if source.is_a?(Symbol) # Register
args = get_arg(scope,left,:save)
atype = args[0] # FIXME: Ugly, but the compiler can't yet compile atype,aparem = get_arg ...
aparam = args[1]
atype = :addr if atype == :possible_callm
if atype == :addr || atype == :cvar
scope.add_constant(aparam)
prefix = scope.name
aparam = prefix + "__" + aparam.to_s if !prefix.empty?
@global_constants << aparam
elsif atype == :ivar
# FIXME: The register allocation here
# probably ought to happen in #save_to_instance_var
@e.popl(source) if source.is_a?(Symbol)
@e.pushl(source)
ret = compile_eval_arg(scope, :self)
@e.with_register do |reg|
@e.popl(reg)
@e.save_to_instance_var(reg, ret, aparam)
end
# FIXME: Need to check for "special" ivars
return Value.new([:subexpr], :object)
end
# FIXME: Otherwise, "source" register may already have been reused
if source.is_a?(Symbol)
@e.popl(:eax)
source = :eax
end
r = @e.save(atype, source, aparam)
if !r
err_msg = "Expected an argument on left hand side of assignment - got #{atype.to_s}, (left: #{left.inspect}, right: #{right.inspect})"
error(err_msg, scope, [:assign, left, right]) # pass current expression as well
end
return Value.new([:subexpr], :object)
end
# Compiles a do-end block expression.
def compile_do(scope, *exp)
if exp.length == 0
exp = [:nil]
end
source = nil
exp.each do |e|
source=compile_eval_arg(scope, e)
@e.save_result(source)
end
return Value.new([:subexpr])
end
# :sexp nodes are just aliases for :do nodes except
# that code that rewrites the tree and don't want to
# affect %s() escaped code should avoid descending
# into :sexp nodes.
def compile_sexp(scope, *exp)
# We explicitly delete the type information for :sexp nodes for now.
Value.new(compile_do(SexpScope.new(scope), *exp), nil)
end
# :block nodes are "begin .. end" blocks or "do .. end" blocks
# (which doesn't really matter to the compiler, just the parser
# - what matters is that if it stands on it's own it will be
# "executed" immediately; otherwise it should be treated like
# a :lambda more or less.
#
# FIXME: Since we don't implement "rescue" yet, we'll just
# treat it as a :do, which is likely to cause lots of failures
def compile_block(scope, *exp)
compile_do(scope, *exp[1])
end
# Compile a literal Array initalization
#
# FIXME: An alternative is another "transform" step
#
def compile_array(scope, *initializers)
compile_eval_arg(scope,
[:callm, :Array, :[], initializers]
)
return Value.new([:subexpr], :object)
end
# Compiles an 8-bit array indexing-expression.
# Takes the current scope, the array as well as the index number to access.
def compile_bindex(scope, arr, index)
source = compile_eval_arg(scope, arr)
@e.pushl(source)
source = compile_eval_arg(scope, index)
r = @e.with_register do |reg|
@e.popl(reg)
@e.save_result(source)
@e.addl(@e.result_value, reg)
end
return Value.new([:indirect8, r])
end
# Compiles a 32-bit array indexing-expression.
# Takes the current scope, the array as well as the index number to access.
def compile_index(scope, arr, index)
source = compile_eval_arg(scope, arr)
r = @e.with_register do |reg|
@e.movl(source, reg)
if index.is_a?(Numeric)
if index != 0
@e.addl(index*4, reg)
end
else
@e.pushl(reg)
source = compile_eval_arg(scope, index)
@e.save_result(source)
@e.sall(2, @e.result_value)
@e.popl(reg)
@e.addl(@e.result_value, reg)
end
end
return Value.new([:indirect, r], lookup_type(arr,index))
end
def let(scope,*varlist, &block)
vars = Hash[*(varlist.zip(1..varlist.size)).flatten]
lscope =LocalVarScope.new(vars, scope)
if varlist.size > 0
@e.evict_regs_for(varlist)
# FIXME: I'm not actually sure why we need to add 1 here.
# FIXME: @bug workaround for @e.with_local(vars.size+1) getting
# turned into (callm @e with_local(callm (calm vars size) + 1))
# (probable parser bug that leaves argument without parentheses
# when single argument given
s = vars.size + 2
# FIXME: @bug: calling "with_local" does not work here, so trying
# to avoid with "with_stack" (and adding 1 extra to var.size above.
# Original line: @e.with_local(vars.size+1) do
@e.with_stack(s) do
block.call(lscope)
end
@e.evict_regs_for(varlist)
else
yield(lscope)
end
end
# Compiles a let expression.
# Takes the current scope, a list of variablenames as well as a list of arguments.
def compile_let(scope, varlist, *args)
let(scope, *varlist) do |ls|
compile_do(ls, *args)
end
return Value.new([:subexpr])
end
# Put at the start of a required file, to allow any special processing
# before/after
def compile_required(scope,exp)
@e.include(exp.position.filename) do
v = scope.get_arg(:__FILE__)
if v[0] == :global
compile_eval_arg(scope,[:assign, :__FILE__, [:sexp, [:__get_string,exp.position.filename]]])
end
ret = compile_exp(scope,exp)
# FIXME: This of course doesn't do what it is intended
# - it needs to reset filename back to its previous value.
if v[0] == :global
compile_eval_arg(scope,[:assign, :__FILE__, [:sexp, [:call, :__get_string,exp.position.filename]]])
end
ret
end
end
# General method for compiling expressions.
# Calls the specialized compile methods depending of the
# expression to be compiled (e.g. compile_if, compile_call, compile_let etc.).
def compile_exp(scope, exp)
return Value.new([:subexpr]) if !exp || exp.size == 0
# FIXME:
# rescue is unsupported in:
# pos = exp.position rescue nil
#
pos = nil
if exp.respond_to?(:position)
pos = exp.position
end
if pos && exp[0] != :defm
@e.lineno(pos) if pos
end
#trace(pos,exp)
# check if exp is within predefined keywords list
## FIXME: Attempt at fixing segfault
cmd = nil
r = nil
exp
if(@@keywords.include?(exp[0]))
# FIXME: This variation segfaults
return self.send("compile_#{exp[0].to_s}", scope, *exp.rest)
#exp
#cmd = "compile_#{exp[0].to_s}"
#if cmd == "compile_defm"
# FIXME: Uncommenting this causes crash to move elsewhere.
#STDERR.puts scope.object_id
# r = exp.rest
# return self.compile_defm(scope, *r)
#end
#return self.send(cmd, scope, *exp.rest)
elsif @@oper_methods.member?(exp[0])
return compile_callm(scope, exp[1], exp[0], exp[2..-1])
else
return compile_call(scope, exp[1], exp[2],exp[3]) if (exp[0] == :call)
return compile_callm(scope, exp[1], exp[2], exp[3], exp[4]) if (exp[0] == :callm)
return compile_call(scope, exp[0], exp.rest) if (exp.is_a? Array)
end
warning("Somewhere calling #compile_exp when they should be calling #compile_eval_arg? #{exp.inspect}")
res = compile_eval_arg(scope, exp[0])
@e.save_result(res)
return Value.new([:subexpr])
end
# Compiles the main function, where the compiled programm starts execution.
def compile_main(exp)
@e.main(exp.position.filename) do
# We should allow arguments to main
# so argc and argv get defined, but
# that is for later.
compile_eval_arg(@global_scope, [:sexp, [:assign, :__stack_top, [:stackframe]]])
compile_eval_arg(@global_scope, exp)
compile_eval_arg(@global_scope, [:sexp,[:exit, 0]])
nil
end
end
# We need to ensure we find the maximum
# size of the vtables *before* we compile
# any of the classes
#
# Consider whether to check :call/:callm nodes as well, though they
# will likely hit method_missing
def alloc_vtable_offsets(exp)
exp.depth_first(:defm) do |defun|
@vtableoffsets.alloc_offset(defun[1])
:skip
end
@vtableoffsets.vtable.each do |name, off|
@e.emit(".equ __voff__#{clean_method_name(name)}, #{off*4}")
end
classes = 0
exp.depth_first(:class) { |c| classes += 1; :skip }
#warning("INFO: Max vtable offset when compiling is #{@vtableoffsets.max} in #{classes} classes, for a total vtable overhead of #{@vtableoffsets.max * classes * 4} bytes")
end
# When we hit a vtable slot for a method that doesn't exist for
# the current object/class, we call method_missing. However, method
# missing needs the symbol of the method that was being called.
#
# To handle that, we insert the address of a "thunk" instead of
# the real method missing. The thunk is a not-quite-function that
# adjusts the stack to prepend the symbol matching the current
# vtable slot and then jumps straight to __method_missing, instead
# of wasting extra stack space and time on copying the objects.
def output_vtable_thunks
@e.label("__vtable_thunks_helper")
@e.popl(:ebx) # numargs
@e.movl("4(%esp)",:esi) # self
# Making space for the symbolx for the method.
@e.movl("(%esp)", :ecx) # Return address
@e.pushl(:ecx)
# Self into new position
@e.movl(:esi, "4(%esp)")
# Block into new position
@e.movl("12(%esp)",:ecx)
@e.movl(:ecx, "8(%esp)")
# Symbol as first argument
@e.movl(:eax,"12(%esp)")
# Adjust argument count
@e.addl(1,:ebx)
load_class(@global_scope)
@e.jmp("*__voff__method_missing(%eax)")
@e.label("__vtable_thunks_start")
@vtableoffsets.vtable.each do |name,_|
@e.label("__vtable_missing_thunk_#{clean_method_name(name)}")
@e.pushl(:ebx)
# FIXME: Call get_symbol for these during initalization
# and then load them from a table instead.
@e.save_result(compile_eval_arg(@global_scope, ":#{name.to_s}".to_sym))
@e.jmp("__vtable_thunks_helper")
end
@e.label("__vtable_thunks_end")
@e.label("__base_vtable")
# For ease of implementation of __new_class_object we
# pad this with the number of class ivar slots so that the
# vtable layout is identical as for a normal class
ClassScope::CLASS_IVAR_NUM.times { @e.long(0) }
@vtableoffsets.vtable.to_a.sort_by {|e| e[1] }.each do |e|
@e.long("__vtable_missing_thunk_#{clean_method_name(e[0])}")
end
end
def output_vtable_names
@e.emit(".equ", "__vtable_size, "[email protected]_s)
@e.label("__vtable_names")
ClassScope::CLASS_IVAR_NUM.times { @e.long(0) }
@vtableoffsets.vtable.to_a.sort_by {|e| e[1] }.each do |e|
sc = strconst(e[0].to_s)
@e.emit(".long", sc.last)
end
@e.comment("")
end
# Starts the actual compile process.
def compile exp
alloc_vtable_offsets(exp)
compile_main(exp)
# after the main function, we ouput all functions and constants
# used and defined so far.
output_functions
output_vtable_thunks
output_vtable_names
output_constants
@e.flush
end
end