IRBuilderTest.st
changeset 44 840c68a91cdd
parent 43 c8afb8e4c3cc
child 45 04a50b0d540a
equal deleted inserted replaced
43:c8afb8e4c3cc 44:840c68a91cdd
     1 "{ Package: 'ctu:ircompiler' }"
       
     2 
       
     3 TestCase subclass:#IRBuilderTest
       
     4 	instanceVariableNames:''
       
     5 	classVariableNames:'TestToPush'
       
     6 	poolDictionaries:''
       
     7 	category:'IR Compiler-Tests'
       
     8 !
       
     9 
       
    10 
       
    11 !IRBuilderTest class methodsFor:'as yet unclassified'!
       
    12 
       
    13 testToPush
       
    14 	^TestToPush
       
    15 !
       
    16 
       
    17 testToPush: anObject
       
    18 
       
    19 	TestToPush := anObject
       
    20 ! !
       
    21 
       
    22 !IRBuilderTest methodsFor:'mock methods'!
       
    23 
       
    24 mock1: arg1 with: arg2
       
    25 
       
    26     ^arg1 + arg2
       
    27 
       
    28     "Created: / 02-12-2008 / 09:11:46 / Jan Vrany <vranyj1@fel.cvut.cz>"
       
    29 !
       
    30 
       
    31 mock2
       
    32 
       
    33     ^[ testSelector ]
       
    34 
       
    35     "
       
    36     decompiling IRBuilderTest>>mock2
       
    37     nA: 0 nV: 0 nT: 2   
       
    38     
       
    39     1: 37 04 00 00             makeBlock 4 (7) nv=0 na=0
       
    40     5: 5A                      pushInstVar1
       
    41     6: 00                      retTop
       
    42     7: 08 03                   LINE[3]
       
    43     9: 00                      retTop
       
    44     "
       
    45 
       
    46     "Created: / 28-03-2009 / 18:47:17 / Jan Vrany <vranyj1@fel.cvut.cz>"
       
    47     "Modified: / 28-03-2009 / 20:16:08 / Jan Vrany <vranyj1@fel.cvut.cz>"
       
    48 !
       
    49 
       
    50 mock3
       
    51     | b |
       
    52     b := 1.
       
    53     ^[ b ]
       
    54 
       
    55     "
       
    56     decompiling IRBuilderTest>>mock3
       
    57     nA: 0 nV: 1 nT: 2
       
    58 
       
    59     1: 79                      push1
       
    60     2: 64                      storeMethodVar1
       
    61     3: 37 04 00 00             makeBlock 4 (9) nv=0 na=0
       
    62     7: 50                      pushMethodVar1
       
    63     8: 00                      retTop
       
    64     9: 08 04                   LINE[4]
       
    65     11:00                      retTop
       
    66     "
       
    67 
       
    68     "Created: / 28-03-2009 / 20:16:36 / Jan Vrany <vranyj1@fel.cvut.cz>"
       
    69 !
       
    70 
       
    71 mock4
       
    72 
       
    73     ^[:arg| arg ]
       
    74 
       
    75     "
       
    76     decompiling IRBuilderTest>>mock4
       
    77     nA: 0 nV: 0 nT: 2
       
    78 
       
    79     1: 37 04 00 01             makeBlock 4 (7) nv=0 na=1
       
    80     5: 8C                      pushBlockArg1
       
    81     6: 00                      retTop
       
    82     7: 08 03                   LINE[3]
       
    83     9: 00                      retTop
       
    84 
       
    85 
       
    86 
       
    87     "
       
    88 
       
    89     "Created: / 28-03-2009 / 20:18:32 / Jan Vrany <vranyj1@fel.cvut.cz>"
       
    90 !
       
    91 
       
    92 mock5
       
    93 
       
    94     ^[|temp| temp ]
       
    95 
       
    96     "
       
    97     decompiling IRBuilderTest>>mock5
       
    98     nA: 0 nV: 0 nT: 2
       
    99     
       
   100     1: 37 04 01 00             makeBlock 4 (7) nv=1 na=0
       
   101     5: E8                      pushBlockVar1
       
   102     6: 00                      retTop
       
   103     7: 08 03                   LINE[3]
       
   104     9: 00                      retTop
       
   105     "
       
   106 
       
   107     "Created: / 28-03-2009 / 20:19:17 / Jan Vrany <vranyj1@fel.cvut.cz>"
       
   108 !
       
   109 
       
   110 mock6
       
   111 
       
   112     ^[|temp| [ temp ] ]
       
   113 
       
   114     "
       
   115     decompiling IRBuilderTest>>mock6
       
   116     nA: 0 nV: 0 nT: 3
       
   117 
       
   118     1: 37 0B 01 00             makeBlock 11 (14) nv=1 na=0
       
   119     5: 37 06 00 00             makeBlock 6 (13) nv=0 na=0
       
   120     9: 80 01 01                pushOuterBlockVar 1 lvl: 1
       
   121     12:00                      retTop
       
   122     13:00                      retTop
       
   123     14:08 03                   LINE[3]
       
   124     16:00                      retTop
       
   125 
       
   126     "
       
   127 
       
   128     "Created: / 28-03-2009 / 20:20:06 / Jan Vrany <vranyj1@fel.cvut.cz>"
       
   129 !
       
   130 
       
   131 mock7
       
   132     | b |
       
   133     b := 1.
       
   134     ^[ ^b ]
       
   135 
       
   136     "
       
   137     decompiling IRBuilderTest>>mock7
       
   138     nA: 0 nV: 1 nT: 2
       
   139 
       
   140     1: 79                      push1
       
   141     2: 64                      storeMethodVar1
       
   142     3: 37 06 00 00             makeBlock 6 (11) nv=0 na=0
       
   143     7: 50                      pushMethodVar1
       
   144     8: 08 04                   LINE[4]
       
   145     10:07                      homeRetTop
       
   146     11:08 04                   LINE[4]
       
   147     13:00                      retTop
       
   148 
       
   149     "
       
   150 
       
   151     "Created: / 28-03-2009 / 20:21:51 / Jan Vrany <vranyj1@fel.cvut.cz>"
       
   152 !
       
   153 
       
   154 mock8
       
   155 
       
   156     ^([:arg| arg ] value: 22)
       
   157 
       
   158     "Created: / 30-03-2009 / 19:21:48 / Jan Vrany <vranyj1@fel.cvut.cz>"
       
   159 !
       
   160 
       
   161 mock9
       
   162 
       
   163     ^[:barg1 | [:barg2 | barg2 ] value: barg1 ]
       
   164         value: 22
       
   165 
       
   166     "
       
   167     decompiling IRBuilderTest>>mock9
       
   168     nA: 0 nV: 0 nT: 3
       
   169     
       
   170      1: 37 0C 00 01             makeBlock 12 (15) nv=0 na=1
       
   171      5: 37 04 00 01             makeBlock 4 (11) nv=0 na=1
       
   172      9: 8C                      pushBlockArg1
       
   173     10: 00                      retTop
       
   174     11: 8C                      pushBlockArg1
       
   175     12: 98 03                   send1 #value:[3]
       
   176     14: 00                      retTop
       
   177     15: 10 16                   pushNum 22
       
   178     17: 98 04                   send1 #value:[4]
       
   179     19: 08 03                   LINE[3]
       
   180     21: 00 
       
   181     
       
   182     "
       
   183 
       
   184     "Created: / 11-05-2009 / 23:09:57 / Jan Vrany <vranyj1@fel.cvut.cz>"
       
   185 ! !
       
   186 
       
   187 !IRBuilderTest methodsFor:'testing'!
       
   188 
       
   189 halt
       
   190         "Redefinition for testing the #send:toSuperOf:"
       
   191 
       
   192     "Created: / 11-06-2008 / 16:08:52 / Jan Vrany <vranyj1@fel.cvut.cz>"
       
   193 !
       
   194 
       
   195 isThisEverCalled
       
   196 	"Redefinition for testing the #send:toSuperOf:"
       
   197 !
       
   198 
       
   199 testDup
       
   200     |iRMethod aCompiledMethod|
       
   201 
       
   202     iRMethod := (IRBuilder new)
       
   203                 numRargs:1;
       
   204                 addTemps:#( #self );
       
   205                 pushLiteral:3;
       
   206                 pushDup;
       
   207                 send:#'=';
       
   208                 returnTop;
       
   209                 ir.
       
   210     aCompiledMethod := iRMethod compiledCode.
       
   211     self assert:(aCompiledMethod isKindOf:CompiledMethod).
       
   212     self 
       
   213         assert:((aCompiledMethod valueWithReceiver:nil arguments:#()) = true).
       
   214 !
       
   215 
       
   216 testInstVar
       
   217     |aCompiledMethod irBuilder|
       
   218 
       
   219     irBuilder := (IRBuilder new)
       
   220                 numRargs:1;
       
   221                 addTemps:#( #self );
       
   222                 pushInstVar:1;
       
   223                 pushInstVar:2;
       
   224                 send:#'+';
       
   225                 returnTop;
       
   226                 ir.
       
   227     aCompiledMethod := irBuilder compiledCode.
       
   228     self assert:(aCompiledMethod isKindOf:CompiledMethod).
       
   229     self 
       
   230         assert:((aCompiledMethod valueWithReceiver:(3 @ 4) arguments:#()) = 7).
       
   231 
       
   232     "Modified: / 11-06-2008 / 13:16:41 / Jan Vrany <vranyj1@fel.cvut.cz>"
       
   233 !
       
   234 
       
   235 testJumpAheadTo
       
   236     |iRMethod aCompiledMethod|
       
   237 
       
   238     iRMethod := (IRBuilder new)
       
   239                 numRargs:1;
       
   240                 addTemps:#( #self );
       
   241                 pushTemp:#self;
       
   242                 jumpAheadTo:#end;
       
   243                 pushLiteral:3;
       
   244                 jumpAheadTarget:#end;
       
   245                 returnTop;
       
   246                 ir.
       
   247     aCompiledMethod := iRMethod compiledCode.
       
   248     self assert:(aCompiledMethod isKindOf:CompiledMethod).
       
   249     self assert:((aCompiledMethod valueWithReceiver:nil arguments:#()) = nil).
       
   250 !
       
   251 
       
   252 testJumpAheadToIf
       
   253     |iRMethod aCompiledMethod|
       
   254 
       
   255     iRMethod := (IRBuilder new)
       
   256                 numRargs:1;
       
   257                 addTemps:#( #self );
       
   258                 pushTemp:#self;
       
   259                 pushLiteral:true;
       
   260                 jumpAheadTo:#end if:true;
       
   261                 pushLiteral:3;
       
   262                 jumpAheadTarget:#end;
       
   263                 returnTop;
       
   264                 ir.
       
   265     aCompiledMethod := iRMethod compiledCode.
       
   266     self assert:(aCompiledMethod isKindOf:CompiledMethod).
       
   267     self assert:((aCompiledMethod valueWithReceiver:nil arguments:#()) = nil).
       
   268 !
       
   269 
       
   270 testJumpBackTo
       
   271     |iRMethod aCompiledMethod|
       
   272 
       
   273     iRMethod := (IRBuilder new)
       
   274                 numRargs:1;
       
   275                 addTemps:#( #self );
       
   276                 pushTemp:#self;
       
   277                 pushLiteral:false;
       
   278                 jumpBackTarget:#begin;
       
   279                 jumpAheadTo:#end if:true;
       
   280                 pushLiteral:true;
       
   281                 jumpBackTo:#begin;
       
   282                 jumpAheadTarget:#end;
       
   283                 returnTop;
       
   284                 ir.
       
   285     aCompiledMethod := iRMethod compiledCode.
       
   286     self assert:(aCompiledMethod isKindOf:CompiledMethod).
       
   287     self assert:((aCompiledMethod valueWithReceiver:nil arguments:#()) = nil).
       
   288 !
       
   289 
       
   290 testLine1
       
   291     |iRMethod aCompiledMethod|
       
   292 
       
   293     iRMethod := (IRBuilder new)
       
   294                 numRargs:1;
       
   295                 addTemps:#( #self );
       
   296                 line:5;
       
   297                 pushLiteral:true;
       
   298                 returnTop;
       
   299                 ir.
       
   300     aCompiledMethod := iRMethod compiledCode.
       
   301     self assert:(aCompiledMethod isKindOf:CompiledMethod).
       
   302     self 
       
   303         assert:((aCompiledMethod valueWithReceiver:nil arguments:#()) = true).
       
   304 
       
   305     "Created: / 02-12-2008 / 09:11:06 / Jan Vrany <vranyj1@fel.cvut.cz>"
       
   306 !
       
   307 
       
   308 testLine2
       
   309     |iRMethod aCompiledMethod|
       
   310 
       
   311     iRMethod := (IRBuilder new)
       
   312                 numRargs:1;
       
   313                 addTemps:#( #self );
       
   314                 line:5;
       
   315                 pushLiteral:true;
       
   316                 pushLiteral:false;
       
   317                 send:#'&';
       
   318                 returnTop;
       
   319                 ir.
       
   320     aCompiledMethod := iRMethod compiledCode.
       
   321     self assert:(aCompiledMethod isKindOf:CompiledMethod).
       
   322     self 
       
   323         assert:((aCompiledMethod valueWithReceiver:nil arguments:#()) = false).
       
   324 
       
   325     "Created: / 02-12-2008 / 09:11:42 / Jan Vrany <vranyj1@fel.cvut.cz>"
       
   326 !
       
   327 
       
   328 testLiteralArray
       
   329     |iRMethod aCompiledMethod|
       
   330 
       
   331     iRMethod := (IRBuilder new)
       
   332                 numRargs:1;
       
   333                 addTemps:#( #self );
       
   334                 pushLiteral:#( #test 4 #you );
       
   335                 returnTop;
       
   336                 ir.
       
   337     aCompiledMethod := iRMethod compiledCode.
       
   338     self assert:(aCompiledMethod isKindOf:CompiledMethod).
       
   339     self assert:((aCompiledMethod valueWithReceiver:nil arguments:#()) 
       
   340                 = #( #test 4 #you )).
       
   341 !
       
   342 
       
   343 testLiteralBoolean
       
   344     |iRMethod aCompiledMethod|
       
   345 
       
   346     iRMethod := (IRBuilder new)
       
   347                 numRargs:1;
       
   348                 addTemps:#( #self );
       
   349                 pushLiteral:true;
       
   350                 returnTop;
       
   351                 ir.
       
   352     aCompiledMethod := iRMethod compiledCode.
       
   353     self assert:(aCompiledMethod isKindOf:CompiledMethod).
       
   354     self 
       
   355         assert:((aCompiledMethod valueWithReceiver:nil arguments:#()) = true).
       
   356 !
       
   357 
       
   358 testLiteralCharacter
       
   359     |iRMethod aCompiledMethod|
       
   360 
       
   361     iRMethod := (IRBuilder new)
       
   362                 numRargs:1;
       
   363                 addTemps:#( #self );
       
   364                 pushLiteral:$e;
       
   365                 returnTop;
       
   366                 ir.
       
   367     aCompiledMethod := iRMethod compiledCode.
       
   368     self assert:(aCompiledMethod isKindOf:CompiledMethod).
       
   369     self assert:((aCompiledMethod valueWithReceiver:nil arguments:#()) = $e).
       
   370 !
       
   371 
       
   372 testLiteralFloat
       
   373     |iRMethod aCompiledMethod|
       
   374 
       
   375     iRMethod := (IRBuilder new)
       
   376                 numRargs:1;
       
   377                 addTemps:#( #self );
       
   378                 pushLiteral:2.0;
       
   379                 returnTop;
       
   380                 ir.
       
   381     aCompiledMethod := iRMethod compiledCode.
       
   382     self assert:(aCompiledMethod isKindOf:CompiledMethod).
       
   383     self assert:((aCompiledMethod valueWithReceiver:nil arguments:#()) = 2.0).
       
   384 
       
   385     "Modified: / 03-11-2008 / 08:39:52 / Jan Vrany <vranyj1@fel.cvut.cz>"
       
   386 !
       
   387 
       
   388 testLiteralInteger
       
   389     |iRMethod aCompiledMethod|
       
   390 
       
   391     iRMethod := (IRBuilder new)
       
   392                 numRargs:1;
       
   393                 addTemps:#( #self );
       
   394                 pushLiteral:2;
       
   395                 returnTop;
       
   396                 ir.
       
   397     aCompiledMethod := iRMethod compiledCode.
       
   398     self assert:(aCompiledMethod isKindOf:CompiledMethod).
       
   399     self assert:((aCompiledMethod valueWithReceiver:nil arguments:#()) = 2).
       
   400 !
       
   401 
       
   402 testLiteralNil
       
   403     |iRMethod aCompiledMethod|
       
   404 
       
   405     iRMethod := (IRBuilder new)
       
   406                 numRargs:1;
       
   407                 addTemps:#( #self );
       
   408                 pushLiteral:nil;
       
   409                 returnTop;
       
   410                 ir.
       
   411     aCompiledMethod := iRMethod compiledCode.
       
   412     self assert:(aCompiledMethod isKindOf:CompiledMethod).
       
   413     self assert:((aCompiledMethod valueWithReceiver:4 arguments:#()) = nil).
       
   414 !
       
   415 
       
   416 testLiteralString
       
   417     |iRMethod aCompiledMethod|
       
   418 
       
   419     iRMethod := (IRBuilder new)
       
   420                 numRargs:1;
       
   421                 addTemps:#( #self );
       
   422                 pushLiteral:'hello';
       
   423                 returnTop;
       
   424                 ir.
       
   425     aCompiledMethod := iRMethod compiledCode.
       
   426     self assert:(aCompiledMethod isKindOf:CompiledMethod).
       
   427     self 
       
   428         assert:((aCompiledMethod valueWithReceiver:nil arguments:#()) = 'hello').
       
   429 !
       
   430 
       
   431 testLiteralSymbole
       
   432     |iRMethod aCompiledMethod|
       
   433 
       
   434     iRMethod := (IRBuilder new)
       
   435                 numRargs:1;
       
   436                 addTemps:#( #self );
       
   437                 pushLiteral:#you;
       
   438                 returnTop;
       
   439                 ir.
       
   440     aCompiledMethod := iRMethod compiledCode.
       
   441     self assert:(aCompiledMethod isKindOf:CompiledMethod).
       
   442     self 
       
   443         assert:((aCompiledMethod valueWithReceiver:nil arguments:#()) = #you).
       
   444 !
       
   445 
       
   446 testLiteralVariableClass
       
   447     |iRMethod aCompiledMethod|
       
   448 
       
   449     iRMethod := (IRBuilder new)
       
   450                 numRargs:1;
       
   451                 addTemps:#( #self );
       
   452                 pushLiteralVariable:Object binding;
       
   453                 returnTop;
       
   454                 ir.
       
   455     aCompiledMethod := iRMethod compiledCode.
       
   456     self assert:(aCompiledMethod isKindOf:CompiledMethod).
       
   457     self 
       
   458         assert:((aCompiledMethod valueWithReceiver:nil arguments:#()) = Object).
       
   459 !
       
   460 
       
   461 testLiteralVariableClassVariable
       
   462     |iRMethod aCompiledMethod|
       
   463 
       
   464     iRMethod := (IRBuilder new)
       
   465                 numRargs:1;
       
   466                 addTemps:#( #self );
       
   467                 pushLiteralVariable:(ArithmeticValue bindingOf:#ArithmeticSignal);
       
   468                 returnTop;
       
   469                 ir.
       
   470     aCompiledMethod := iRMethod compiledCode.
       
   471     self assert:(aCompiledMethod isKindOf:CompiledMethod).
       
   472     self assert:((aCompiledMethod valueWithReceiver:nil arguments:#()) 
       
   473                 = ArithmeticValue arithmeticSignal).
       
   474 
       
   475     "Modified: / 11-06-2008 / 11:31:32 / Jan Vrany <vranyj1@fel.cvut.cz>"
       
   476 !
       
   477 
       
   478 testLiteralVariableGlobale
       
   479     |iRMethod aCompiledMethod|
       
   480 
       
   481     iRMethod := (IRBuilder new)
       
   482                 numRargs:1;
       
   483                 addTemps:#( #self );
       
   484                 pushLiteralVariable:Smalltalk binding;
       
   485                 returnTop;
       
   486                 ir.
       
   487     aCompiledMethod := iRMethod compiledCode.
       
   488     self assert:(aCompiledMethod isKindOf:CompiledMethod).
       
   489     self 
       
   490         assert:((aCompiledMethod valueWithReceiver:nil arguments:#()) = Smalltalk).
       
   491 
       
   492     "Modified: / 11-06-2008 / 11:32:07 / Jan Vrany <vranyj1@fel.cvut.cz>"
       
   493 !
       
   494 
       
   495 testPopTop
       
   496     |iRMethod aCompiledMethod|
       
   497 
       
   498     iRMethod := (IRBuilder new)
       
   499                 numRargs:1;
       
   500                 addTemps:#( #self );
       
   501                 pushLiteral:true;
       
   502                 pushLiteral:false;
       
   503                 popTop;
       
   504                 returnTop;
       
   505                 ir.
       
   506     aCompiledMethod := iRMethod compiledCode.
       
   507     self assert:(aCompiledMethod isKindOf:CompiledMethod).
       
   508     self 
       
   509         assert:((aCompiledMethod valueWithReceiver:nil arguments:#()) = true).
       
   510 
       
   511     "Modified: / 11-06-2008 / 13:22:11 / Jan Vrany <vranyj1@fel.cvut.cz>"
       
   512 !
       
   513 
       
   514 testPushReceiver
       
   515     |iRMethod aCompiledMethod receiver|
       
   516 
       
   517     iRMethod := (IRBuilder new)
       
   518                 numRargs:1;
       
   519                 addTemps:#( #self );
       
   520                 pushReceiver;
       
   521                 returnTop;
       
   522                 ir.
       
   523     aCompiledMethod := iRMethod compiledCode.
       
   524     receiver := (5 @ 8).
       
   525     self assert:(aCompiledMethod isKindOf:CompiledMethod).
       
   526     self assert:((aCompiledMethod valueWithReceiver:receiver arguments:#()) 
       
   527                 == receiver).
       
   528 !
       
   529 
       
   530 testPushTempArgument
       
   531     |iRMethod aCompiledMethod|
       
   532 
       
   533     iRMethod := (IRBuilder new)
       
   534                 numRargs:3;
       
   535                 addTemps:#( #self #a #b );
       
   536                 pushTemp:#a;
       
   537                 pushTemp:#b;
       
   538                 send:#'+';
       
   539                 returnTop;
       
   540                 ir.
       
   541     aCompiledMethod := iRMethod compiledCode.
       
   542     self assert:(aCompiledMethod isKindOf:CompiledMethod).
       
   543     self 
       
   544         assert:((aCompiledMethod valueWithReceiver:nil arguments:#( 2 8 )) = 10).
       
   545 !
       
   546 
       
   547 testPushTempSelf
       
   548     |iRMethod aCompiledMethod|
       
   549 
       
   550     iRMethod := (IRBuilder new)
       
   551                 numRargs:1;
       
   552                 addTemps:#( #self );
       
   553                 pushTemp:#self;
       
   554                 send:#class;
       
   555                 returnTop;
       
   556                 ir.
       
   557     aCompiledMethod := iRMethod compiledCode.
       
   558     self assert:(aCompiledMethod isKindOf:CompiledMethod).
       
   559     self assert:((aCompiledMethod valueWithReceiver:nil arguments:#()) 
       
   560                 == UndefinedObject).
       
   561 !
       
   562 
       
   563 testPushTempTemp
       
   564     |iRMethod aCompiledMethod|
       
   565 
       
   566     iRMethod := (IRBuilder new)
       
   567                 numRargs:1;
       
   568                 addTemps:#( #self #a );
       
   569                 pushTemp:#a;
       
   570                 returnTop;
       
   571                 ir.
       
   572     aCompiledMethod := iRMethod compiledCode.
       
   573     self assert:(aCompiledMethod isKindOf:CompiledMethod).
       
   574     self assert:((aCompiledMethod valueWithReceiver:5 arguments:#()) = nil).
       
   575 !
       
   576 
       
   577 testPushThisContext
       
   578     |iRMethod aCompiledMethod|
       
   579 
       
   580     iRMethod := (IRBuilder new)
       
   581                 numRargs:1;
       
   582                 addTemps:#( #self #a );
       
   583                 pushThisContext;
       
   584                 send:#receiver;
       
   585                 returnTop;
       
   586                 ir.
       
   587     aCompiledMethod := iRMethod compiledCode.
       
   588     self assert:(aCompiledMethod isKindOf:CompiledMethod).
       
   589     self assert:((aCompiledMethod valueWithReceiver:5 arguments:#()) = 5).
       
   590 !
       
   591 
       
   592 testPushThisEnv
       
   593     |iRMethod aCompiledMethod receiver|
       
   594 
       
   595     ^ self.
       
   596     iRMethod := (IRBuilder new)
       
   597                 numRargs:1;
       
   598                 addTemps:#( #self );
       
   599                 pushThisContext;
       
   600                 pushLiteral:5;
       
   601                 pushLiteral:ClosureEnvironment;
       
   602                 pushLiteral:1;
       
   603                 send:#new:;
       
   604                 send:#privSetInstVar:put:;
       
   605                 pushThisEnv;
       
   606                 returnTop;
       
   607                 ir.
       
   608     aCompiledMethod := iRMethod compiledCode.
       
   609     receiver := Object new.
       
   610     self assert:(aCompiledMethod isKindOf:CompiledMethod).
       
   611     self assert:((aCompiledMethod valueWithReceiver:receiver arguments:#()) 
       
   612                 isKindOf:ClosureEnvironment)
       
   613 !
       
   614 
       
   615 testSendNumArgs1
       
   616     |iRMethod aCompiledMethod|
       
   617 
       
   618     iRMethod := (IRBuilder new)
       
   619                 numRargs:1;
       
   620                 addTemps:#( #self );
       
   621                 pushReceiver;
       
   622                 pushLiteral:1;
       
   623                 pushLiteral:2;
       
   624                 send:#mock1 numArgs:2;
       
   625                 returnTop;
       
   626                 ir.
       
   627     aCompiledMethod := iRMethod compiledCode.
       
   628     self class basicAddSelector:#mock1
       
   629         withMethod:(self class >> #mock1:with:).
       
   630     self assert:(aCompiledMethod isKindOf:CompiledMethod).
       
   631     self 
       
   632         assert:(aCompiledMethod valueWithReceiver:(IRBuilderTest new)
       
   633                 arguments:#()) = 3.
       
   634     self class basicRemoveSelector:#mock1.
       
   635 
       
   636     "Created: / 01-12-2008 / 19:58:18 / Jan Vrany <vranyj1@fel.cvut.cz>"
       
   637 !
       
   638 
       
   639 testSendNumArgs2
       
   640     |iRMethod aCompiledMethod|
       
   641 
       
   642     iRMethod := (IRBuilder new)
       
   643                 numRargs:1;
       
   644                 addTemps:#( #self );
       
   645                 pushReceiver;
       
   646                 pushLiteral:1;
       
   647                 send:#mock1 numArgs:1;
       
   648                 returnTop;
       
   649                 ir.
       
   650     aCompiledMethod := iRMethod compiledCode.
       
   651     self class basicAddSelector:#mock1
       
   652         withMethod:(self class >> #mock1:with:).
       
   653     self assert:(aCompiledMethod isKindOf:CompiledMethod).
       
   654     self 
       
   655         should:[
       
   656             (aCompiledMethod valueWithReceiver:(IRBuilderTest new) arguments:#())
       
   657         ]
       
   658         raise:Error.
       
   659     self class basicRemoveSelector:#mock1.
       
   660 
       
   661     "Created: / 01-12-2008 / 19:59:02 / Jan Vrany <vranyj1@fel.cvut.cz>"
       
   662 !
       
   663 
       
   664 testSendSuper
       
   665     |iRMethod aCompiledMethod|
       
   666 
       
   667     iRMethod := (IRBuilder new)
       
   668                 numRargs:1;
       
   669                 addTemps:#( #self );
       
   670                 pushReceiver;
       
   671                 send:#halt toSuperOf:IRBuilderTest;
       
   672                 returnTop;
       
   673                 ir.
       
   674     aCompiledMethod := iRMethod compiledCode.
       
   675     self assert:(aCompiledMethod isKindOf:CompiledMethod).
       
   676     self 
       
   677         should:[
       
   678             (aCompiledMethod valueWithReceiver:(IRBuilderTest new) arguments:#())
       
   679         ]
       
   680         raise:Error.
       
   681 
       
   682     "Modified: / 11-06-2008 / 16:09:12 / Jan Vrany <vranyj1@fel.cvut.cz>"
       
   683 !
       
   684 
       
   685 testStorIntoVariable
       
   686     |iRMethod aCompiledMethod|
       
   687 
       
   688     iRMethod := (IRBuilder new)
       
   689                 numRargs:1;
       
   690                 addTemps:#( #self );
       
   691                 pushLiteral:4;
       
   692                 storeIntoLiteralVariable:(IRBuilderTest bindingOf:#TestToPush);
       
   693                 returnTop;
       
   694                 ir.
       
   695     aCompiledMethod := iRMethod compiledCode.
       
   696     self assert:(aCompiledMethod isKindOf:CompiledMethod).
       
   697     aCompiledMethod valueWithReceiver:nil arguments:#().
       
   698     self assert:(IRBuilderTest testToPush = 4).
       
   699     IRBuilderTest testToPush:nil.
       
   700 !
       
   701 
       
   702 testStoreTemp
       
   703     |iRMethod aCompiledMethod|
       
   704 
       
   705     iRMethod := (IRBuilder new)
       
   706                 numRargs:1;
       
   707                 addTemps:#( #self #a );
       
   708                 pushLiteral:34;
       
   709                 storeTemp:#a;
       
   710                 pushTemp:#a;
       
   711                 returnTop;
       
   712                 ir.
       
   713     aCompiledMethod := iRMethod compiledCode.
       
   714     self assert:(aCompiledMethod isKindOf:CompiledMethod).
       
   715     self assert:((aCompiledMethod valueWithReceiver:nil arguments:#()) = 34).
       
   716 
       
   717     "Modified: / 11-06-2008 / 16:24:43 / Jan Vrany <vranyj1@fel.cvut.cz>"
       
   718 !
       
   719 
       
   720 testStoreThisEnv
       
   721     |iRMethod aCompiledMethod|
       
   722 
       
   723     ^ self.
       
   724     iRMethod := (IRBuilder new)
       
   725                 numRargs:1;
       
   726                 addTemps:#( #self #a );
       
   727                 pushLiteral:ClosureEnvironment;
       
   728                 pushLiteral:1;
       
   729                 send:#new:;
       
   730                 storeThisEnv;
       
   731                 pushThisContext;
       
   732                 pushLiteral:5;
       
   733                 send:#privGetInstVar:;
       
   734                 returnTop;
       
   735                 ir.
       
   736     aCompiledMethod := iRMethod compiledCode.
       
   737     self assert:(aCompiledMethod isKindOf:CompiledMethod).
       
   738     self assert:((aCompiledMethod valueWithReceiver:nil arguments:#()) 
       
   739                 isKindOf:ClosureEnvironment).
       
   740 
       
   741     "Modified: / 11-06-2008 / 14:47:51 / Jan Vrany <vranyj1@fel.cvut.cz>"
       
   742 !
       
   743 
       
   744 testTwoJumpAheadToIfsToSameTarget
       
   745     |iRMethod aCompiledMethod|
       
   746 
       
   747     iRMethod := (IRBuilder new)
       
   748                 numRargs:1;
       
   749                 addTemps:#( #self );
       
   750                 pushTemp:#self;
       
   751                 pushLiteral:false;
       
   752                 jumpAheadTo:#end if:true;
       
   753                 pushLiteral:true;
       
   754                 jumpAheadTo:#end if:true;
       
   755                 pushLiteral:3;
       
   756                 jumpAheadTarget:#end;
       
   757                 returnTop;
       
   758                 ir.
       
   759     aCompiledMethod := iRMethod compiledCode.
       
   760     self assert:(aCompiledMethod isKindOf:CompiledMethod).
       
   761     self assert:((aCompiledMethod valueWithReceiver:nil arguments:#()) = nil).
       
   762 
       
   763     "Created: / 03-11-2008 / 13:34:50 / Jan Vrany <vranyj1@fel.cvut.cz>"
       
   764 ! !
       
   765 
       
   766 !IRBuilderTest methodsFor:'testing - blocks'!
       
   767 
       
   768 testBlock_blockNesting_1
       
   769     |aCompiledMethod irBuilder|
       
   770 
       
   771     irBuilder := (IRBuilder new)
       
   772                 numRargs:1;
       
   773                 addTemps:#( #self );
       
   774                 pushBlockUsingBuilder:[:builder | 
       
   775                         builder
       
   776                             numRargs:1;
       
   777                             addTemps:#( #barg1 );
       
   778                             pushBlockUsingBuilder:[:builder|
       
   779                                 builder
       
   780                                     numRargs: 1;
       
   781                                     addTemps: #( #barg2 );
       
   782                                     pushTemp: #barg2;
       
   783                                     returnTop
       
   784                             ];
       
   785                             pushTemp: #barg1;
       
   786                             send: #value:;
       
   787                             returnTop
       
   788                     ];
       
   789                 pushLiteral: 22;
       
   790                 send:#value;
       
   791                 returnTop;
       
   792                 ir.
       
   793     "
       
   794         irBuilder ir
       
   795     "
       
   796 
       
   797     aCompiledMethod := irBuilder compiledCode.
       
   798     self assert:(aCompiledMethod isKindOf:CompiledMethod).
       
   799     self assert:((aCompiledMethod valueWithReceiver:1 arguments:#()) = 22).
       
   800 
       
   801     "Created: / 11-05-2009 / 23:04:13 / Jan Vrany <vranyj1@fel.cvut.cz>"
       
   802 !
       
   803 
       
   804 testBlock_blockTempArg
       
   805     |aCompiledMethod irBuilder|
       
   806 
       
   807     irBuilder := (IRBuilder new)
       
   808                 numRargs:1;
       
   809                 addTemps:#( #self );
       
   810                 pushBlockUsingBuilder:[:builder | 
       
   811                         builder
       
   812                             numRargs:1;
       
   813                             addTemps:#( #barg1 );
       
   814                             pushTemp:#barg1;
       
   815                             returnTop
       
   816                     ];
       
   817                 pushLiteral:22;
       
   818                 send:#value:;
       
   819                 returnTop;
       
   820                 ir.
       
   821     "
       
   822         irBuilder ir
       
   823     "
       
   824 
       
   825     aCompiledMethod := irBuilder compiledCode.
       
   826     self assert:(aCompiledMethod isKindOf:CompiledMethod).
       
   827     self assert:((aCompiledMethod valueWithReceiver:1 arguments:#()) = 22).
       
   828 
       
   829     "Created: / 30-03-2009 / 14:26:18 / Jan Vrany <vranyj1@fel.cvut.cz>"
       
   830     "Modified: / 30-03-2009 / 19:16:25 / Jan Vrany <vranyj1@fel.cvut.cz>"
       
   831 !
       
   832 
       
   833 testBlock_blockTempVar
       
   834     |aCompiledMethod irBuilder|
       
   835 
       
   836     irBuilder := (IRBuilder new)
       
   837                 numRargs:1;
       
   838                 addTemps:#( #self );
       
   839                 pushBlockUsingBuilder:[:builder | 
       
   840                         builder
       
   841                             numRargs:0;
       
   842                             addTemps:#( #bvar1 );
       
   843                             pushLiteral: 22;
       
   844                             storeTemp:#bvar1;
       
   845                             pushTemp: #bvar1;
       
   846                             returnTop
       
   847                     ];
       
   848                 send:#value;
       
   849                 returnTop;
       
   850                 ir.
       
   851     "
       
   852         irBuilder ir
       
   853     "
       
   854 
       
   855     aCompiledMethod := irBuilder compiledCode.
       
   856     self assert:(aCompiledMethod isKindOf:CompiledMethod).
       
   857     self assert:((aCompiledMethod valueWithReceiver:1 arguments:#()) = 22).
       
   858 
       
   859     "Created: / 30-03-2009 / 23:03:48 / Jan Vrany <vranyj1@fel.cvut.cz>"
       
   860 !
       
   861 
       
   862 testBlock_block_in_another_basic_block
       
   863     |aCompiledMethod irBuilder|
       
   864 
       
   865     irBuilder := (IRBuilder new)
       
   866                 numRargs:1;
       
   867                 addTemps:#( #self );
       
   868                 pushLiteral: false;
       
   869                 pushLiteral: true;
       
   870                 jumpAheadTo: #end if: false;
       
   871                 pushBlockUsingBuilder:[:builder | 
       
   872                         builder
       
   873                             numRargs: 0;
       
   874                             pushLiteral: true;
       
   875                             returnTop
       
   876                     ];
       
   877                 send: #value;
       
   878                 jumpAheadTarget: #end;
       
   879                 returnTop;
       
   880                 ir.
       
   881     "
       
   882         irBuilder ir
       
   883     "
       
   884 
       
   885     aCompiledMethod := irBuilder compiledCode.
       
   886     self assert:(aCompiledMethod isKindOf:CompiledMethod).
       
   887     self assert:((aCompiledMethod valueWithReceiver:1 arguments:#()) = true).
       
   888 
       
   889     "Created: / 13-05-2009 / 11:24:48 / Jan Vrany <vranyj1@fel.cvut.cz>"
       
   890 !
       
   891 
       
   892 testBlock_exceptionHandler
       
   893     |aCompiledMethod irBuilder|
       
   894 
       
   895     irBuilder := (IRBuilder new)
       
   896                 numRargs:1;
       
   897                 addTemps:#( #self);
       
   898                pushBlockUsingBuilder:[:builder | 
       
   899                         builder
       
   900                             numRargs:0;  
       
   901                             pushLiteral: 1;
       
   902                             pushLiteral: 0;
       
   903                             send: #/;
       
   904                             pushLiteral: false;
       
   905                             remoteReturn
       
   906                     ];
       
   907                 pushLiteralVariable: #Error;
       
   908                 pushBlockUsingBuilder:[:builder|
       
   909                     builder
       
   910                         numRargs:0;
       
   911                         pushLiteral: true;
       
   912                         remoteReturn];
       
   913                 send: #on:do:;
       
   914                 pushLiteral: false;
       
   915                 returnTop;
       
   916                 ir.
       
   917     "
       
   918         irBuilder ir
       
   919     "
       
   920 
       
   921     aCompiledMethod := irBuilder compiledCode.
       
   922     self assert:(aCompiledMethod isKindOf:CompiledMethod).
       
   923     self assert:((aCompiledMethod valueWithReceiver:1 arguments:#()) = true).
       
   924 
       
   925     "Created: / 11-05-2009 / 21:53:19 / Jan Vrany <vranyj1@fel.cvut.cz>"
       
   926 !
       
   927 
       
   928 testBlock_exceptionHandler_noRemoteReturn
       
   929     |aCompiledMethod irBuilder|
       
   930 
       
   931     irBuilder := (IRBuilder new)
       
   932                 numRargs:1;
       
   933                 addTemps:#( #self);
       
   934                pushBlockUsingBuilder:[:builder | 
       
   935                         builder
       
   936                             numRargs:0;  
       
   937                             pushLiteral: 1;
       
   938                             pushLiteral: 0;
       
   939                             send: #/;
       
   940                             pushLiteral: false;
       
   941                             returnTop
       
   942                     ];
       
   943                 pushLiteralVariable: #Error;
       
   944                 pushBlockUsingBuilder:[:builder|
       
   945                     builder
       
   946                         numRargs:0;
       
   947                         pushLiteral: true;
       
   948                         returnTop];
       
   949                 send: #on:do:;
       
   950                 returnTop;
       
   951                 ir.
       
   952     "
       
   953         irBuilder ir
       
   954     "
       
   955 
       
   956     aCompiledMethod := irBuilder compiledCode.
       
   957     self assert:(aCompiledMethod isKindOf:CompiledMethod).
       
   958     self assert:((aCompiledMethod valueWithReceiver:1 arguments:#()) = true).
       
   959 
       
   960     "Created: / 11-05-2009 / 21:53:25 / Jan Vrany <vranyj1@fel.cvut.cz>"
       
   961 !
       
   962 
       
   963 testBlock_invalid_instruction_ordering_bug_1
       
   964     |aCompiledMethod irBuilder|
       
   965 
       
   966     irBuilder := (IRBuilder new)
       
   967                     numRargs: 1;
       
   968                     addTemps: #(self i);
       
   969                     pushTemp: #i;
       
   970                     pushLiteral: 3;
       
   971                     pushReceiver;
       
   972 
       
   973                     pushBlockUsingBuilder: [ :builder | 
       
   974                         builder
       
   975                             numRargs: 0;
       
   976                             addTemps: #();
       
   977                             pushTemp: #i;
       
   978                             returnTop.
       
   979                     ];
       
   980                     returnTop;
       
   981                     ir.
       
   982     "
       
   983         irBuilder ir
       
   984     "
       
   985 
       
   986     aCompiledMethod := irBuilder compiledCode.
       
   987     self assert:(aCompiledMethod isKindOf:CompiledMethod).
       
   988     self assert:((aCompiledMethod valueWithReceiver:1 arguments:#()) isKindOf: Block).
       
   989 
       
   990     "Created: / 11-05-2009 / 21:55:30 / Jan Vrany <vranyj1@fel.cvut.cz>"
       
   991 !
       
   992 
       
   993 testBlock_methodTempVar
       
   994     |aCompiledMethod irBuilder|
       
   995 
       
   996     irBuilder := (IRBuilder new)
       
   997                 numRargs:1;
       
   998                 addTemps:#( #self #a);
       
   999                 pushLiteral: 22;
       
  1000                 storeTemp: #a;
       
  1001                 pushBlockUsingBuilder:[:builder | 
       
  1002                         builder
       
  1003                             numRargs:0;
       
  1004                             pushTemp: #a;
       
  1005                             returnTop
       
  1006                     ];
       
  1007                 send:#value;
       
  1008                 returnTop;
       
  1009                 ir.
       
  1010     "
       
  1011         irBuilder ir
       
  1012     "
       
  1013 
       
  1014     aCompiledMethod := irBuilder compiledCode.
       
  1015     self assert:(aCompiledMethod isKindOf:CompiledMethod).
       
  1016     self assert:((aCompiledMethod valueWithReceiver:1 arguments:#()) = 22).
       
  1017 
       
  1018     "Created: / 30-03-2009 / 23:05:00 / Jan Vrany <vranyj1@fel.cvut.cz>"
       
  1019     "Modified: / 01-04-2009 / 19:43:52 / Jan Vrany <vranyj1@fel.cvut.cz>"
       
  1020 ! !
       
  1021 
       
  1022 !IRBuilderTest class methodsFor:'documentation'!
       
  1023 
       
  1024 version_CVS
       
  1025     ^ 'Header: /cvs/stx/cvut/stx/goodies/newcompiler/IRBuilderTest.st,v 1.3 2009/10/08 12:03:51 fm Exp '
       
  1026 !
       
  1027 
       
  1028 version_SVN
       
  1029     ^ '$Id::                                                                                                                        $'
       
  1030 ! !
       
  1031