forked from rubinius/rubinius
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfield_extract.rb
1329 lines (1109 loc) · 34.5 KB
/
field_extract.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
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
class BasicPrimitive
attr_accessor :pass_state
attr_accessor :pass_self
attr_accessor :pass_call_frame
attr_accessor :pass_message
attr_accessor :pass_arguments
attr_accessor :pass_gctoken
attr_accessor :raw
attr_accessor :safe
attr_accessor :can_fail
def output_header(str)
str << "Object* Primitives::#{@name}(STATE, CallFrame* call_frame, Executable* exec, Module* mod, Arguments& args) {\n"
str << " state->set_call_frame(call_frame);\n"
str << " Object* ret;\n"
return str if @raw
str << " Object* self;\n" if @pass_self
str << " GCTokenImpl gct;\n" if @pass_gctoken
end
def output_args(str, arg_types)
unless @pass_arguments
str << " if(unlikely(args.total() != #{arg_types.size}))\n"
str << " goto fail;\n\n"
end
args = []
i = -1
arg_types.each do |t|
i += 1
case t
when :arguments
args << "args"
else
str << " #{t}* a#{i};\n"
str << " a#{i} = try_as<#{t}>(args.get_argument(#{i}));\n"
str << " if(unlikely(a#{i} == NULL))\n"
str << " goto fail;\n\n"
args << "a#{i}"
end
end
args.unshift "gct" if @pass_gctoken
args.unshift "self" if @pass_self
args.unshift "state" if @pass_state
if @pass_message
args.push "exec"
args.push "mod"
end
args.push "call_frame" if @pass_call_frame
return args
end
def prim_return(str, indent=2)
str << "#{' ' * indent}return ret;\n"
end
def output_call(str, call, args)
str << "\n"
str << " try {\n"
str << "#ifdef RBX_PROFILER\n"
str << " if(unlikely(state->vm()->tooling())) {\n"
str << " tooling::MethodEntry method(state, exec, mod, args);\n"
str << " RUBINIUS_METHOD_PRIMITIVE_ENTRY_HOOK(state, mod, args.name(), call_frame);\n"
str << " ret = #{call}(#{args.join(', ')});\n"
str << " RUBINIUS_METHOD_PRIMITIVE_RETURN_HOOK(state, mod, args.name(), call_frame);\n"
str << " } else {\n"
str << " RUBINIUS_METHOD_PRIMITIVE_ENTRY_HOOK(state, mod, args.name(), call_frame);\n"
str << " ret = #{call}(#{args.join(', ')});\n"
str << " RUBINIUS_METHOD_PRIMITIVE_RETURN_HOOK(state, mod, args.name(), call_frame);\n"
str << " }\n"
str << "#else\n"
str << " RUBINIUS_METHOD_PRIMITIVE_ENTRY_HOOK(state, mod, args.name(), call_frame);\n"
str << " ret = #{call}(#{args.join(', ')});\n"
str << " RUBINIUS_METHOD_PRIMITIVE_RETURN_HOOK(state, mod, args.name(), call_frame);\n"
str << "#endif\n"
str << " } catch(const RubyException& exc) {\n"
str << " exc.exception->locations(state,\n"
str << " Location::from_call_stack(state, call_frame));\n"
str << " state->raise_exception(exc.exception);\n"
str << " RUBINIUS_METHOD_PRIMITIVE_RETURN_HOOK(state, mod, args.name(), call_frame);\n"
str << " return NULL;\n"
str << " }\n"
str << "\n"
str << " if(unlikely(ret == reinterpret_cast<Object*>(kPrimitiveFailed)))\n"
str << " goto fail;\n\n"
prim_return(str);
str << "fail:\n"
str << " return CompiledCode::primitive_failed(state, call_frame, exec, mod, args);\n"
str << "}\n\n"
end
end
class CPPPrimitive < BasicPrimitive
def initialize(name, type)
@type = type
@name = name
end
attr_accessor :type, :name
attr_accessor :cpp_name, :return_type, :arg_types
def arg_count
arg_types.size
end
def generate_glue
str = ""
if @raw
arg_count = nil
else
arg_count = arg_types.size
end
output_header str
str << " #{@type}* recv = try_as<#{@type}>(args.recv());\n"
str << " if(unlikely(recv == NULL)) goto fail;\n"
if @raw
str << "\n"
str << " try {\n"
str << " ret = recv->#{@cpp_name}(state, call_frame, exec, mod, args);\n"
str << " } catch(const RubyException& exc) {\n"
str << " exc.exception->locations(state,\n"
str << " Location::from_call_stack(state, call_frame));\n"
str << " state->raise_exception(exc.exception);\n"
str << " return NULL;\n"
str << " }\n"
str << " if(ret == Primitives::failure()) goto fail;\n"
str << " return ret;\n"
str << "\n"
str << "fail:\n"
str << " return CompiledCode::primitive_failed(state, call_frame, exec, mod, args);\n"
str << "}\n\n"
else
args = output_args str, arg_types
str << " self = recv;\n" if @pass_self
output_call str, "recv->#{@cpp_name}", args
end
return str
end
def generate_jit_stub
return if @raw or @pass_message or @pass_arguments
# Default value, overriden below
@can_fail = true
str = ""
if arg_types.empty?
arg_list = ""
else
list = []
arg_types.size.times { |i| list << "Object* ra#{i}" }
arg_list = ", " + list.join(", ")
end
if @safe && !@pass_call_frame
str << "extern \"C\" Object* jit_stub_#{@name}(STATE, Object* recv #{arg_list}) {\n"
else
str << "extern \"C\" Object* jit_stub_#{@name}(STATE, CallFrame* call_frame, Object* recv #{arg_list}) {\n"
end
str << " Object* ret;\n"
str << " GCTokenImpl gct;\n" if @pass_gctoken
emit_fail = false
i = 0
arg_types.each do |t|
str << " #{t}* a#{i};\n"
i += 1
end
if @type == "Object"
str << " Object* self = recv;\n"
else
emit_fail = true
str << " #{@type}* self = try_as<#{@type}>(recv);\n"
str << " if(unlikely(self == NULL)) goto fail;\n"
end
args = []
i = 0
arg_types.each do |t|
emit_fail = true
str << " a#{i} = try_as<#{t}>(ra#{i});\n"
str << " if(unlikely(a#{i} == NULL)) goto fail;\n"
args << "a#{i}"
i += 1
end
unless @safe
str << " state->set_call_frame(call_frame);\n"
end
args.unshift "gct" if @pass_gctoken
args.unshift "recv" if @pass_self
args.unshift "state" if @pass_state
args.push "call_frame" if @pass_call_frame
if @safe
str << " ret = self->#{@cpp_name}(#{args.join(', ')});\n"
str << " return ret;\n"
else
str << "\n"
str << " try {\n"
str << " ret = self->#{@cpp_name}(#{args.join(', ')});\n"
str << " } catch(const RubyException& exc) {\n"
str << " exc.exception->locations(state,\n"
str << " Location::from_call_stack(state, call_frame));\n"
str << " state->raise_exception(exc.exception);\n"
str << " return NULL;\n"
str << " }\n"
str << "\n"
str << " if(unlikely(ret == reinterpret_cast<Object*>(kPrimitiveFailed)))\n"
str << " goto fail;\n\n"
str << " return ret;\n"
end
if !@safe or emit_fail
str << "fail:\n"
str << " return cUndef;\n"
end
if @safe and !emit_fail
@can_fail = false
end
str << "}\n\n"
end
def generate_invoke_stub
return if @raw or @pass_arguments
str = ""
if arg_types.empty?
arg_list = ""
else
list = []
arg_types.size.times { |i| list << "Object* ra#{i}" }
arg_list = ", " + list.join(", ")
end
str << "extern \"C\" Object* invoke_#{@name}(STATE, CallFrame* call_frame, Object** args, int arg_count) {\n"
str << " Object* ret;\n"
str << " GCTokenImpl gct;\n" if @pass_gctoken
i = 0
arg_types.each do |t|
str << " #{t}* a#{i};\n"
i += 1
end
arity = arg_types.size + 1
if @type == "Object"
str << " Object* self;\n"
else
str << " #{@type}* self;\n"
end
str << " if(arg_count != #{arity}) goto fail;\n"
if @type == "Object"
str << " self = args[0];\n"
else
str << " self = try_as<#{@type}>(args[0]);\n"
str << " if(unlikely(self == NULL)) goto fail;\n"
end
args = []
i = 0
arg_types.each do |t|
emit_fail = true
str << " a#{i} = try_as<#{t}>(args[#{i + 1}]);\n"
str << " if(unlikely(a#{i} == NULL)) goto fail;\n"
args << "a#{i}"
i += 1
end
args.unshift "gct" if @pass_gctoken
args.unshift "recv" if @pass_self
args.unshift "state" if @pass_state
args.push "call_frame" if @pass_call_frame
str << "\n"
str << " try {\n"
str << " ret = self->#{@cpp_name}(#{args.join(', ')});\n"
str << " } catch(const RubyException& exc) {\n"
str << " exc.exception->locations(state,\n"
str << " Location::from_call_stack(state, call_frame));\n"
str << " state->raise_exception(exc.exception);\n"
str << " return NULL;\n"
str << " }\n"
str << "\n"
str << " if(unlikely(ret == reinterpret_cast<Object*>(kPrimitiveFailed)))\n"
str << " goto fail;\n\n"
str << " return ret;\n"
str << "fail:\n"
str << " Exception::internal_error(state, call_frame, \"invoked primitive failed\");"
str << " return 0;\n"
str << "}\n\n"
end
end
class CPPStaticPrimitive < CPPPrimitive
def generate_glue
str = ""
output_header str
if @raw
str << "\n"
str << " try {\n"
str << " return #{@type}::#{@cpp_name}(state, exec, call_frame, mod);\n"
str << " } catch(const RubyException& exc) {\n"
str << " exc.exception->locations(state,\n"
str << " Location::from_call_stack(state, call_frame));\n"
str << " state->raise_exception(exc.exception);\n"
str << " return NULL;\n"
str << " }\n"
str << "\n"
str << "}\n\n"
else
args = output_args str, arg_types
str << " self = args.recv();\n" if @pass_self
output_call str, "#{@type}::#{@cpp_name}", args
end
return str
end
def generate_jit_stub
return if @raw or @pass_arguments
# Default value, overriden below
@can_fail = true
str = ""
if arg_types.empty?
arg_list = ""
else
list = []
arg_types.size.times { |i| list << "Object* ra#{i}" }
arg_list = ", " + list.join(", ")
end
if @safe && !@pass_call_frame
str << "extern \"C\" Object* jit_stub_#{@name}(STATE, Object* recv #{arg_list}) {\n"
else
str << "extern \"C\" Object* jit_stub_#{@name}(STATE, CallFrame* call_frame, Object* recv #{arg_list}) {\n"
end
str << " Object* ret;\n"
str << " GCTokenImpl gct;\n" if @pass_gctoken
i = 0
arg_types.each do |t|
str << " #{t}* a#{i};\n"
i += 1
end
emit_fail = false
args = []
i = 0
arg_types.each do |t|
emit_fail = true
str << " a#{i} = try_as<#{t}>(ra#{i});\n"
str << " if(unlikely(a#{i} == NULL)) goto fail;\n"
args << "a#{i}"
i += 1
end
args.unshift "gct" if @pass_gctoken
args.unshift "recv" if @pass_self
args.unshift "state" if @pass_state
args.push "call_frame" if @pass_call_frame
if @safe
str << " ret = #{@type}::#{@cpp_name}(#{args.join(', ')});\n"
str << " return ret;\n"
else
str << "\n"
str << " try {\n"
str << " ret = #{@type}::#{@cpp_name}(#{args.join(', ')});\n"
str << " } catch(const RubyException& exc) {\n"
str << " exc.exception->locations(state,\n"
str << " Location::from_call_stack(state, call_frame));\n"
str << " state->raise_exception(exc.exception);\n"
str << " return NULL;\n"
str << " }\n"
str << "\n"
str << " if(unlikely(ret == reinterpret_cast<Object*>(kPrimitiveFailed)))\n"
str << " goto fail;\n\n"
str << " return ret;\n"
end
if !@safe or emit_fail
str << "fail:\n"
str << " return cUndef;\n"
end
if @safe and !emit_fail
@can_fail = false
end
str << "}\n\n"
end
def generate_invoke_stub
return if @raw or @pass_arguments
str = ""
if arg_types.empty?
arg_list = ""
else
list = []
arg_types.size.times { |i| list << "Object* ra#{i}" }
arg_list = ", " + list.join(", ")
end
str << "extern \"C\" Object* invoke_#{@name}(STATE, CallFrame* call_frame, Object** args, int arg_count) {\n"
str << " Object* ret;\n"
str << " GCTokenImpl gct;\n" if @pass_gctoken
i = 0
arg_types.each do |t|
str << " #{t}* a#{i};\n"
i += 1
end
arity = arg_types.size
arity += 1 if @pass_self
str << " if(arg_count != #{arity}) goto fail;\n"
args = []
i = 0
arg_types.each do |t|
str << " a#{i} = try_as<#{t}>(args[#{i}]);\n"
str << " if(unlikely(a#{i} == NULL)) goto fail;\n"
args << "a#{i}"
i += 1
end
args.unshift "gct" if @pass_gctoken
args.unshift "args[arg_count-1]" if @pass_self
args.unshift "state" if @pass_state
args.push "call_frame" if @pass_call_frame
str << "\n"
str << " try {\n"
str << " ret = #{@type}::#{@cpp_name}(#{args.join(', ')});\n"
str << " } catch(const RubyException& exc) {\n"
str << " exc.exception->locations(state,\n"
str << " Location::from_call_stack(state, call_frame));\n"
str << " state->raise_exception(exc.exception);\n"
str << " return NULL;\n"
str << " }\n"
str << "\n"
str << " if(unlikely(ret == reinterpret_cast<Object*>(kPrimitiveFailed)))\n"
str << " goto fail;\n\n"
str << " return ret;\n"
str << "fail:\n"
str << " Exception::internal_error(state, call_frame, \"invoked primitive failed\");"
str << " return 0;\n"
str << "}\n\n"
end
end
class CPPOverloadedPrimitive < BasicPrimitive
def initialize(prim)
@name = prim.name
@type = prim.type
@cpp_name = prim.cpp_name
@kinds = [prim]
end
def add_kind(prim)
if prim.arg_types.size != 1
raise "Invalid overloaded primitive: Only 2nd arg can be overloaded."
end
@kinds << prim
end
def generate_glue
str = ""
output_header str
str << " #{@type}* recv = try_as<#{@type}>(args.recv());\n"
str << " if(likely(recv)) {\n"
str << " if(args.total() != 1) goto fail;\n"
@kinds.each do |prim|
type = prim.arg_types.first
str << " if(#{type}* arg = try_as<#{type}>(args.get_argument(0))) {\n"
str << " try {\n"
if @pass_state
call = " ret = recv->#{@cpp_name}(state, arg);\n"
else
call = " ret = recv->#{@cpp_name}(arg);\n"
end
str << "#ifdef RBX_PROFILER\n"
str << " if(unlikely(state->vm()->tooling())) {\n"
str << " tooling::MethodEntry method(state, exec, mod, args);\n"
str << " " << call
str << " } else {\n"
str << " " << call
str << " }\n"
str << "#else\n"
str << call
str << "#endif\n"
str << " } catch(const RubyException& exc) {\n"
str << " exc.exception->locations(state,\n"
str << " Location::from_call_stack(state, call_frame));\n"
str << " state->raise_exception(exc.exception);\n"
str << " return NULL;\n"
str << " }\n"
str << " if(likely(ret != reinterpret_cast<Object*>(kPrimitiveFailed))) {\n"
prim_return(str, 8);
str << " }\n"
str << " }\n"
str << "\n"
end
str << " }\n"
str << "fail:\n"
str << " return CompiledCode::primitive_failed(state, call_frame, exec, mod, args);\n"
str << "}\n\n"
return str
end
end
class CPPClass
def initialize(name)
@name = name
@super = nil
@fields = []
@flags = {}
@primitives = {}
@num_fields = 0
@subclasses = []
end
attr_accessor :name, :super, :fields, :primitives, :flags, :num_fields
attr_accessor :subclasses
def add_field(idx, name, type, flag=nil)
if flag
@flags[name] = flag
end
@fields << [name, type, idx]
end
def field_index_offset
return @super.all_fields if @super
return 0
end
def all_fields
if @super
ary = @super.all_fields
else
ary = []
end
offset = ary.size
@fields.each do |n,t,i|
if readonly?(n)
flags = {:readonly => true}
else
flags = {}
end
ary << [n, t, i + offset, flags]
end
return ary
end
def access_primitives
all_fields.map do |name, type, offset, flags|
"access_#{@name}_#{name}"
end
end
def readonly?(name)
@flags[name] == "readonly"
end
def add_primitive(name, cpp_name, ret, args, overload=false)
prim = CPPPrimitive.new(name, @name)
prim.cpp_name = cpp_name
prim.return_type = ret
prim.arg_types = args
if cur = @primitives[name]
unless overload
raise "Illegal overloading of primitive #{name}"
end
unless prim.arg_types.size == 1
raise "Overloaded variant of #{name} must only take one arg"
end
if cur.kind_of? CPPOverloadedPrimitive
cur.add_kind prim
return cur
else
over = CPPOverloadedPrimitive.new(cur)
@primitives[name] = over
over.add_kind prim
return over
end
else
@primitives[name] = prim
end
return prim
end
def add_static_primitive(name, cpp_name, ret, args)
prim = CPPStaticPrimitive.new(name, @name)
prim.cpp_name = cpp_name
prim.return_type = ret
prim.arg_types = args
if @primitives[name]
raise "Already defined primitive #{name}"
end
@primitives[name] = prim
return prim
end
def generate_gets
str = ""
all_fields.each do |name, type, idx, flags|
str << " case #{idx}:\n"
str << " return target->#{name}();\n"
end
return str
end
def generate_sets
str = ""
all_fields.each do |name, type, idx, flags|
str << " case #{idx}:\n"
if flags[:readonly]
str << " Exception::assertion_error(state, \"#{name} is readonly\");\n"
else
str << " target->#{name}(state, val->nil_p() ? nil<#{type}>() : as<#{type}>(val));\n"
end
str << " return;\n"
end
return str
end
def generate_accessors
str = ""
all_fields.each do |name, type, idx, flags|
str << "Object* Primitives::access_#{@name}_#{name}(STATE, CallFrame* call_frame, Executable* exec, Module* mod,
Arguments& args) {\n"
str << " AccessVariable* access = as<AccessVariable>(exec);\n"
str << " if(access->write()->true_p()) {\n"
str << <<-ARGS
if(args.total() != 1) {
Exception::argument_error(state, 1, args.total());
return NULL;
}
ARGS
str << " Object* val = args.get_argument(0);\n"
str << " if(val->nil_p()) {\n"
str << " as<#{@name}>(args.recv())->#{name}(state, nil<#{type}>());\n"
str << " } else {\n"
str << " as<#{@name}>(args.recv())->#{name}(state, as<#{type}>(val));\n"
str << " }\n"
str << " return args.get_argument(0);\n"
str << " }\n"
str << <<-ARGS
if(args.total() != 0) {
Exception::argument_error(state, 0, args.total());
return NULL;
}
ARGS
str << " return as<#@name>(args.recv())->#{name}();\n"
str << "}\n\n"
end
return str
end
def generate_typechecks
out = generate_sets()
return '' if out.strip.empty?
<<-EOF
void #{@name}::Info::set_field(STATE, Object* _t, size_t index, Object* val) {
#{@name}* target = as<#{@name}>(_t);
switch(index) {
#{out} }
}
Object* #{@name}::Info::get_field(STATE, Object* _t, size_t index) {
#{@name}* target = as<#{@name}>(_t);
switch(index) {
#{generate_gets} }
std::ostringstream error;
error << "Unable to access field " << index << " in a #{@name} instance";
Exception::assertion_error(state, error.str().c_str());
return cNil; // never reached
}
EOF
end
def generate_marks(cpp)
str = ''
str << generate_marks(cpp.super) if cpp.super
cpp.fields.each do |name, type, idx|
str << <<-EOF
{
if(target->#{name}()->reference_p()) {
Object* old = target->#{name}();
Object* cur = mark.call(old);
if(cur && cur != old) target->#{name}(mark.state(), force_as<#{type}>(cur));
}
}
EOF
end
return str
end
def generate_visits(cpp)
str = ''
str << generate_visits(cpp.super) if cpp.super
cpp.fields.each do |name, type, idx|
str << <<-EOF
visit.call(target->#{name}());
EOF
end
return str
end
def generate_mark
marks = generate_marks(self).rstrip
str = ''
str << <<-EOF unless marks.empty?
void #{@name}::Info::auto_mark(Object* _t, ObjectMark& mark) {
#{@name}* target = as<#{@name}>(_t);
#{marks}
}
EOF
str
end
def kind_of_code(what)
case @name
when "Fixnum"
return "#{what}->fixnum_p()"
when "Symbol"
return "#{what}->symbol_p()"
when "TrueClass"
return "#{what}->true_p()"
when "FalseClass"
return "#{what}->false_p()"
when "NilClass"
return "#{what}->nil_p()"
else
return "(#{what}->reference_p() && #{what}->type_id() == #{@name}Type)"
end
end
def kind_of_conditions(what)
[kind_of_code(what)] + @subclasses.map do |cpp|
cpp.kind_of_conditions(what)
end
end
def generate_kind_of
checks = kind_of_conditions("obj")
# We forward declare the class in here to keep everything happy
str = <<-CPP
template <>
SPECIALIZATION_STORAGE bool kind_of<#{@name}>(const Object* obj) {
return #{checks.join(' || ')};
}
CPP
end
def class_has_object_size
@flags[:object_size] = true
end
def class_has_object_size?
@flags[:object_size]
end
end
class CPPParser
attr_reader :classes, :class_order
def initialize
@classes = Hash.new
@class_order = []
@type_map = {
"InstructionSequence" => :InstructionSequence,
"Symbol" => :Symbol,
"Object" => :Object,
"Tuple" => :Tuple,
"MemoryPointer" => :MemoryPointer,
"ConstantScope" => :ConstantScope,
"Integer" => :Integer,
"Fixnum" => :Fixnum,
"CompiledCode" => :CompiledCode,
"String" => :String,
"Module" => :Module,
"Class" => :Class,
"Float" => :Float,
"Bignum" => :Bignum,
"Hash" => :Hash,
"Channel" => :Channel
}
end
def add_class(name, obj)
@class_order << name unless @class_order.include? name
@classes[name] = obj
end
def strip_and_map(type, map)
if type[-1] == ?*
type = type[0..-2]
end
if val = map[type]
return val
end
raise "Unable to resolve type '#{type}'"
end
def parse_stream(f)
class_pattern = /class\s+([^\s]+)\s*:\s*public\s+([^\s]+)/
slot_pattern = %r!^\s*(\w+)\*?\s+\*?(\w+)_\s*;\s*//\s*slot(.*)!
primitive_pattern = %r%^\s*//\s+Rubinius.primitive([?!\+])?\s+:(.*)\s*$%
prototype_pattern = %r!\s*(static\s+)?([\w\*]+)\s+([\w]+)\((.*)\)!
object_size_pattern = %r|size_t\s+object_size\s*\(const\s+ObjectHeader\s*|
while l = f.gets
next unless m = class_pattern.match(l)
if m[2] != "Object" and m[2] != "ObjectHeader"
# We've found a class definition that doesn't extend Object.
# If we recognize the superclass, make a new CPPClass instance
# for this class definition.
if sup = @classes[m[2]]
cpp = CPPClass.new(m[1])
cpp.super = sup
sup.subclasses << cpp
else
next
end
else
# A class definition directly subclassing Object or ObjectHeader
cpp = CPPClass.new(m[1])
end
add_class(cpp.name, cpp)
@type_map[cpp.name] = cpp.name.to_sym
# We are finished with this class if it was a one-liner.
next if /\};/.match(l)
# Otherwise, keep scanning to look for fields and primitives
idx = 0
while l = f.gets
break if /^\s*class\s+/.match(l)
# Field declarations marked with a "// slot" comment
if m = slot_pattern.match(l)
type = m[1]
name = m[2]
# Optional 'flag' argument after '// slot'
# Currently 'readonly' is the only supported flag
if m[3]
flag = m[3].strip
else
flag = nil
end
field_type = @type_map[type] || type.to_s
cpp.add_field idx, name, field_type, flag
idx += 1
# A primitive declaration marked with '// Rubinius.primitive'
elsif m = primitive_pattern.match(l)
overload = m[1] == "!"
raw = m[1] == "?"
safe = m[1] == "+"
prim = m[2]
prototype = f.gets
pass_state = false
pass_self = false
pass_call_frame = false
pass_message = false
pass_arguments = false
pass_gctoken = false
m = prototype_pattern.match(prototype)
unless m
raise "Unable to parse: '#{prototype}'"
end
args = m[4].split(/\s*,\s*/)
# If the first argument is the +STATE+ macro, handle it in +output_args+
if args.first == "STATE"
args.shift and pass_state = true
if args.first =~ /GCToken .*/
args.shift
pass_gctoken = true
end
# If the second argument is +Object* self+, we will automatically pass
# in the receiver of the primitive message in +output_call+
if args.first == "Object* self"
args.shift and pass_self = true
end
end
if args.last == "CallFrame* calling_environment"
pass_call_frame = true
args.pop
end
if i = args.index("Arguments& args")
pass_arguments = true
args[i] = :arguments
end
if raw then
arg_types = []
else
arg_types = args.map do |a|
case a
when Symbol
a
else
strip_and_map(a.split(/\s+/, 2).first, @type_map)
end
end
end
if m[1]
if overload
raise "Unable to overload static primitives."
end
obj = cpp.add_static_primitive prim, m[3], @type_map[m[2]], arg_types
else
obj = cpp.add_primitive prim, m[3], @type_map[m[2]], arg_types, overload
end