Advertisement
Guest User

Untitled

a guest
Jun 11th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.58 KB | None | 0 0
  1. diff --git a/src/vm/jvm/HLL/Backend.nqp b/src/vm/jvm/HLL/Backend.nqp
  2. index 4c32e70..18dae14 100644
  3. --- a/src/vm/jvm/HLL/Backend.nqp
  4. +++ b/src/vm/jvm/HLL/Backend.nqp
  5. @@ -61,7 +61,7 @@ class HLL::Backend::JVM {
  6.  
  7. method jast($qast, *%adverbs) {
  8. my $classname := %*COMPILING<%?OPTIONS><javaclass> || nqp::sha1('eval-at-' ~ nqp::time_n() ~ $compile_count++);
  9. - nqp::getcomp('QAST').jast($qast, :$classname);
  10. + nqp::getcomp('QAST').jast($qast, :$classname, |%adverbs);
  11. }
  12.  
  13. method classfile($jast, *%adverbs) {
  14. diff --git a/src/vm/jvm/QAST/Compiler.nqp b/src/vm/jvm/QAST/Compiler.nqp
  15. index fd66557..e423b9d 100644
  16. --- a/src/vm/jvm/QAST/Compiler.nqp
  17. +++ b/src/vm/jvm/QAST/Compiler.nqp
  18. @@ -2782,7 +2782,13 @@ QAST::OperationsJAST.map_classlib_core_op('getuniprop_str', $TYPE_OPS, 'getunipr
  19.  
  20. QAST::OperationsJAST.map_classlib_core_op('force_gc', $TYPE_OPS, 'force_gc', [], $RT_OBJ, :tc);
  21.  
  22. -class QAST::CompilerJAST {
  23. +class QAST::CompilerJASTInstance {
  24. + has $!JCLASS;
  25. + has $!NEXT_QBID;
  26. + has %!CUID_TO_QBID;
  27. + has $!HLL;
  28. + has $!COMP_MODE;
  29. +
  30. # Responsible for handling issues around code references, building the
  31. # switch statement dispatcher, etc.
  32. my class CodeRefBuilder {
  33. @@ -2792,14 +2798,16 @@ class QAST::CompilerJAST {
  34. has @!cuids;
  35. has @!callsites;
  36. has %!callsite_map;
  37. + has $!jclass;
  38.  
  39. - method BUILD() {
  40. + method BUILD(:$jclass) {
  41. $!cur_idx := 0;
  42. %!cuid_to_idx := {};
  43. @!jastmeth_names := [];
  44. @!cuids := [];
  45. @!callsites := [];
  46. %!callsite_map := {};
  47. + $!jclass := $jclass;
  48. }
  49.  
  50. method register_method($jastmeth, $cuid) {
  51. @@ -2887,7 +2895,7 @@ class QAST::CompilerJAST {
  52.  
  53. # Return the array. Add method to class.
  54. $csa.append($ARETURN);
  55. - $*JCLASS.add_method($csa);
  56. + $!jclass.add_method($csa);
  57. }
  58. }
  59.  
  60. @@ -3142,15 +3150,23 @@ class QAST::CompilerJAST {
  61. # Set up a JAST::Class that will hold all the blocks (which become Java
  62. # methods) that we shall compile.
  63. my $file := nqp::ifnull(nqp::getlexdyn('$?FILES'), "");
  64. - my $*JCLASS := JAST::Class.new(
  65. + my $!JCLASS := JAST::Class.new(
  66. :name($classname),
  67. :super('org.perl6.nqp.runtime.CompilationUnit'),
  68. :filename($file)
  69. );
  70. + if nqp::existskey(%adverbs, 'mast_frames') {
  71. + if nqp::existskey(%adverbs<mast_frames>, 'jclass') {
  72. + $!JCLASS := nqp::atkey(%adverbs<mast_frames>, 'jclass');
  73. + }
  74. + else {
  75. + nqp::bindkey(%adverbs<mast_frames>, 'jclass', $!JCLASS);
  76. + }
  77. + }
  78.  
  79. # We'll also need to keep track of all the blocks we compile into Java
  80. # methods; the CodeRefBuilder takes care of that.
  81. - my $*CODEREFS := CodeRefBuilder.new();
  82. + my $*CODEREFS := CodeRefBuilder.new(:jclass($!JCLASS));
  83.  
  84. # Now compile $source. By the end of this, the various data structures
  85. # set up above will be fully populated.
  86. @@ -3160,7 +3176,7 @@ class QAST::CompilerJAST {
  87. $*CODEREFS.jastify();
  88.  
  89. # Finally, we hand back the finished class.
  90. - return $*JCLASS
  91. + return $!JCLASS
  92. }
  93.  
  94. # Tracks what is currently on the stack, and what things that were on the
  95. @@ -3307,8 +3323,13 @@ class QAST::CompilerJAST {
  96. }
  97.  
  98. method cuid_to_qbid(str $cuid) {
  99. - my $map := %*CUID_TO_QBID;
  100. - nqp::existskey($map, $cuid) ?? $map{$cuid} !! ($map{$cuid} := $*NEXT_QBID++);
  101. + my $map := %!CUID_TO_QBID;
  102. + if nqp::existskey($map, $cuid) {
  103. + $map{$cuid}
  104. + } else {
  105. + $map{$cuid} := $!NEXT_QBID++;
  106. + $map{$cuid}
  107. + }
  108. }
  109.  
  110. multi method as_jast(QAST::CompUnit $cu, :$want) {
  111. @@ -3316,9 +3337,9 @@ class QAST::CompilerJAST {
  112. my $*EH_IDX := 1;
  113.  
  114. # Set HLL.
  115. - my $*HLL := '';
  116. + my $!HLL := '';
  117. if $cu.hll {
  118. - $*HLL := $cu.hll;
  119. + $!HLL := $cu.hll;
  120. }
  121.  
  122. # Should have a single child which is the outer block.
  123. @@ -3326,13 +3347,14 @@ class QAST::CompilerJAST {
  124. nqp::die("QAST::CompUnit should have one child that is a QAST::Block");
  125. }
  126.  
  127. - my %*CUID_TO_QBID;
  128. - my $*NEXT_QBID := 0;
  129. + my %!CUID_TO_QBID;
  130. + my $!NEXT_QBID := 0;
  131. # Pre-seed to make sure that qbids correspond to serialization IDs
  132. - my $*COMP_MODE := $cu.compilation_mode;
  133. - if $*COMP_MODE {
  134. + my $!COMP_MODE := $cu.compilation_mode;
  135. + if $!COMP_MODE {
  136. for $cu.code_ref_blocks() -> $qblock {
  137. - %*CUID_TO_QBID{$qblock.cuid} := $*NEXT_QBID++;
  138. + %!CUID_TO_QBID{$qblock.cuid} := $!NEXT_QBID++;
  139. + nqp::sayfh(nqp::getstderr(), "cuid {$qblock.cuid} with qbid $!NEXT_QBID");
  140. }
  141. }
  142.  
  143. @@ -3347,6 +3369,7 @@ class QAST::CompilerJAST {
  144. # If we are in compilation mode, or have pre-deserialization or
  145. # post-deserialization tasks, handle those. Overall, the process
  146. # is to desugar this into simpler QAST nodes, then compile those.
  147. + my $is_nested := $cu.is_nested;
  148. my @pre_des := $cu.pre_deserialize;
  149. my @post_des := $cu.post_deserialize;
  150. if %*BLOCK_LEX_VALUES {
  151. @@ -3355,7 +3378,7 @@ class QAST::CompilerJAST {
  152. QAST::Op.new( :op('setup_blv'), %*BLOCK_LEX_VALUES )
  153. ));
  154. }
  155. - if $*COMP_MODE || @pre_des || @post_des || need_set_code_object($cu) {
  156. + if $!COMP_MODE || @pre_des || @post_des || need_set_code_object($cu) {
  157. # Create a block into which we'll install all of the other
  158. # pieces.
  159. my $block := QAST::Block.new( :blocktype('raw') );
  160. @@ -3366,13 +3389,13 @@ class QAST::CompilerJAST {
  161. }
  162.  
  163. # If we need to do deserialization, emit code for that.
  164. - if $*COMP_MODE {
  165. + if $!COMP_MODE && !$is_nested {
  166. $block.push(self.deserialization_code($cu.sc(), $cu.code_ref_blocks(),
  167. $cu.repo_conflict_resolver()));
  168. }
  169.  
  170. # Add code object fixups.
  171. - if $cu.code_ref_blocks() {
  172. + if $cu.code_ref_blocks() && !$is_nested {
  173. my $cur_pd_block := QAST::Block.new( :blocktype('immediate') );
  174. my $i := 0;
  175. for $cu.code_ref_blocks() {
  176. @@ -3414,7 +3437,7 @@ class QAST::CompilerJAST {
  177. my $des_meth := JAST::Method.new( :name('deserializeQbid'), :returns('I'), :static(0) );
  178. $des_meth.append(JAST::PushIndex.new( :value(self.cuid_to_qbid($block.cuid)) ));
  179. $des_meth.append($IRETURN);
  180. - $*JCLASS.add_method($des_meth);
  181. + $!JCLASS.add_method($des_meth);
  182. }
  183.  
  184. # Compile and include load-time logic, if any.
  185. @@ -3428,13 +3451,14 @@ class QAST::CompilerJAST {
  186. my $load_meth := JAST::Method.new( :name('loadQbid'), :returns('I'), :static(0) );
  187. $load_meth.append(JAST::PushIndex.new( :value(self.cuid_to_qbid($load_block.cuid)) ));
  188. $load_meth.append($IRETURN);
  189. - $*JCLASS.add_method($load_meth);
  190. + $!JCLASS.add_method($load_meth);
  191. }
  192.  
  193. # Compile and include main-time logic, if any, and then add a Java
  194. # main that will lead to its invocation.
  195. + my $main_block;
  196. if nqp::defined($cu.main) {
  197. - my $main_block := QAST::Block.new(
  198. + $main_block := QAST::Block.new(
  199. :blocktype('raw'),
  200. $cu.main,
  201. QAST::Op.new( :op('null') )
  202. @@ -3442,33 +3466,33 @@ class QAST::CompilerJAST {
  203. self.as_jast($main_block);
  204. my $main_meth := JAST::Method.new( :name('main'), :returns('Void') );
  205. $main_meth.add_argument('argv', "[$TYPE_STR");
  206. - $main_meth.append(JAST::PushCVal.new( :value('L' ~ $*JCLASS.name ~ ';') ));
  207. + $main_meth.append(JAST::PushCVal.new( :value('L' ~ $!JCLASS.name ~ ';') ));
  208. $main_meth.append(JAST::PushIndex.new( :value(self.cuid_to_qbid($main_block.cuid)) ));
  209. $main_meth.append($ALOAD_0);
  210. $main_meth.append(JAST::Instruction.new( :op('invokestatic'),
  211. $TYPE_CU, 'enterFromMain',
  212. 'Void', 'Ljava/lang/Class;', 'I', "[$TYPE_STR"));
  213. $main_meth.append($RETURN);
  214. - $*JCLASS.add_method($main_meth);
  215. + $!JCLASS.add_method($main_meth);
  216. my $entry_cuid_meth := JAST::Method.new( :name('entryQbid'), :returns('I'), :static(0) );
  217. $entry_cuid_meth.append(JAST::PushIndex.new( :value(self.cuid_to_qbid($main_block.cuid)) ));
  218. $entry_cuid_meth.append($IRETURN);
  219. - $*JCLASS.add_method($entry_cuid_meth);
  220. + $!JCLASS.add_method($entry_cuid_meth);
  221. }
  222.  
  223. # Add method that returns HLL name.
  224. my $hll_meth := JAST::Method.new( :name('hllName'), :returns($TYPE_STR), :static(0) );
  225. - $hll_meth.append(JAST::PushSVal.new( :value($*HLL) ));
  226. + $hll_meth.append(JAST::PushSVal.new( :value($!HLL) ));
  227. $hll_meth.append($ARETURN);
  228. - $*JCLASS.add_method($hll_meth);
  229. + $!JCLASS.add_method($hll_meth);
  230.  
  231. # Add method that returns the mainline block.
  232. my $mainline_meth := JAST::Method.new( :name('mainlineQbid'), :returns('I'), :static(0) );
  233. $mainline_meth.append(JAST::PushIndex.new( :value(self.cuid_to_qbid($cu[0].cuid)) ));
  234. $mainline_meth.append($IRETURN);
  235. - $*JCLASS.add_method($mainline_meth);
  236. + $!JCLASS.add_method($mainline_meth);
  237.  
  238. - return $*JCLASS;
  239. + return $!JCLASS;
  240. }
  241.  
  242. sub need_set_code_object($cu) {
  243. @@ -3486,7 +3510,7 @@ class QAST::CompilerJAST {
  244. my $serialized := nqp::serialize($sc, $sh);
  245.  
  246. if %*COMPILING<%?OPTIONS><target> eq 'jar' {
  247. - $*JCLASS.serialized($serialized);
  248. + $!JCLASS.serialized($serialized);
  249. $serialized := nqp::null();
  250. }
  251.  
  252. @@ -3520,7 +3544,7 @@ class QAST::CompilerJAST {
  253. my $count_meth := JAST::Method.new( :name('serializedCodeRefCount'), :returns('I'), :static(0) );
  254. $count_meth.append(JAST::PushIndex.new( :value(+@code_ref_blocks) ));
  255. $count_meth.append($IRETURN);
  256. - $*JCLASS.add_method($count_meth);
  257. + $!JCLASS.add_method($count_meth);
  258.  
  259. # Overall deserialization QAST.
  260. QAST::Stmts.new(
  261. @@ -3672,8 +3696,9 @@ class QAST::CompilerJAST {
  262. # unique ID and name. (Note, always void return here as return values
  263. # are handled out of band).
  264. my $*JMETH := JAST::Method.new( :name('qb_'~self.cuid_to_qbid($node.cuid)), :returns('Void'), :static(1) );
  265. + nqp::printfh(nqp::getstderr(), "declared JMETH 'qb_{self.cuid_to_qbid($node.cuid)} for cuid {$node.cuid}'\n");
  266. $*JMETH.cr_name($node.name);
  267. - $*JMETH.cr_cuid($node.cuid) unless $*COMP_MODE;
  268. + $*JMETH.cr_cuid($node.cuid) unless $!COMP_MODE;
  269. $*CODEREFS.register_method($*JMETH, $node.cuid);
  270.  
  271. # Set outer if we have one.
  272. @@ -4014,7 +4039,7 @@ class QAST::CompilerJAST {
  273. }
  274.  
  275. # Finalize method and add it to the class.
  276. - $*JCLASS.add_method($*JMETH);
  277. + $!JCLASS.add_method($*JMETH);
  278. }
  279.  
  280. # Now go by block type for producing a result; also need to special-case
  281. @@ -4047,13 +4072,13 @@ class QAST::CompilerJAST {
  282. # Emit the virtual call.
  283. if $args_expectation == $ARG_EXP_NO_ARGS {
  284. $il.append(savesite(JAST::Instruction.new( :op('invokestatic'),
  285. - 'L' ~ $*JCLASS.name ~ ';',
  286. + 'L' ~ $!JCLASS.name ~ ';',
  287. $*CODEREFS.cuid_to_jastmethname($node.cuid),
  288. 'V', $TYPE_CU, $TYPE_TC, $TYPE_CR, $TYPE_CSD, $TYPE_RESUME )));
  289. }
  290. else {
  291. $il.append(savesite(JAST::Instruction.new( :op('invokestatic'),
  292. - 'L' ~ $*JCLASS.name ~ ';',
  293. + 'L' ~ $!JCLASS.name ~ ';',
  294. $*CODEREFS.cuid_to_jastmethname($node.cuid),
  295. 'V', $TYPE_CU, $TYPE_TC, $TYPE_CR, $TYPE_CSD, $TYPE_RESUME, "[$TYPE_OBJ" )));
  296. }
  297. @@ -4144,7 +4169,7 @@ class QAST::CompilerJAST {
  298.  
  299. multi method as_jast(QAST::Op $node, :$want) {
  300. my $hll := '';
  301. - try $hll := $*HLL;
  302. + try $hll := $!HLL;
  303. QAST::OperationsJAST.compile_op(self, $hll, $node);
  304. }
  305.  
  306. @@ -4751,12 +4776,12 @@ class QAST::CompilerJAST {
  307. }
  308. elsif $desired == $RT_OBJ {
  309. my $hll := '';
  310. - try $hll := $*HLL;
  311. + try $hll := $!HLL;
  312. return QAST::OperationsJAST.box(self, $hll, $got);
  313. }
  314. elsif $got == $RT_OBJ {
  315. my $hll := '';
  316. - try $hll := $*HLL;
  317. + try $hll := $!HLL;
  318. return QAST::OperationsJAST.unbox(self, $hll, $desired);
  319. }
  320. elsif $desired == $RT_INT {
  321. @@ -6391,6 +6416,20 @@ class QAST::CompilerJAST {
  322. method operations() { QAST::OperationsJAST }
  323. }
  324.  
  325. +class QAST::CompilerJAST {
  326. + method jast($qast, :$classname, *%adverbs) {
  327. + QAST::CompilerJASTInstance.new.jast($qast, :$classname, |%adverbs)
  328. + }
  329. +
  330. + method operations() {
  331. + QAST::OperationsJAST
  332. + }
  333. +
  334. + method instance() {
  335. + QAST::CompilerJASTInstance
  336. + }
  337. +}
  338. +
  339. # Register as the QAST compiler.
  340. if nqp::isnull(nqp::getcomp('QAST')) {
  341. nqp::bindcomp('QAST', QAST::CompilerJAST);
  342. diff --git a/src/vm/jvm/runtime/org/perl6/nqp/runtime/CompilationUnit.java b/src/vm/jvm/runtime/org/perl6/nqp/runtime/CompilationUnit.java
  343. index 68b5ede..aec6901 100644
  344. --- a/src/vm/jvm/runtime/org/perl6/nqp/runtime/CompilationUnit.java
  345. +++ b/src/vm/jvm/runtime/org/perl6/nqp/runtime/CompilationUnit.java
  346. @@ -69,6 +69,10 @@ public abstract class CompilationUnit {
  347. return cu;
  348. }
  349.  
  350. + public class SomethingFishyException extends RuntimeException {
  351. + public SomethingFishyException(String msg) { super(msg); }
  352. + }
  353. +
  354. /**
  355. * Does initialization work for the compilation unit.
  356. */
  357. @@ -98,7 +102,8 @@ public abstract class CompilationUnit {
  358. cr.st = BOOTCodeSTable;
  359. codeRefList.add(cr);
  360.  
  361. - if (m.qbid >= 0 && m.qbid < qbidToCodeRef.length) qbidToCodeRef[m.qbid] = cr;
  362. + if (m.qbid >= 0 && m.qbid < qbidToCodeRef.length) { qbidToCodeRef[m.qbid] = cr; }
  363. + else { throw new SomethingFishyException("what even"); }
  364.  
  365. /* Stash outer, for later resolution. */
  366. outerCuid.add(ann);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement