IRTransformTest.st
changeset 1 0dd36941955f
child 9 04518c7fb91c
equal deleted inserted replaced
0:de981640a2ec 1:0dd36941955f
       
     1 "{ Package: 'stx:goodies/newcompiler' }"
       
     2 
       
     3 TestCase subclass:#IRTransformTest
       
     4 	instanceVariableNames:''
       
     5 	classVariableNames:''
       
     6 	poolDictionaries:''
       
     7 	category:'NewCompiler-IR-Tests'
       
     8 !
       
     9 
       
    10 
       
    11 !IRTransformTest methodsFor:'testing'!
       
    12 
       
    13 testAdd
       
    14 	
       
    15 	| iRMethod aCompiledMethod |
       
    16 
       
    17 	iRMethod := IRBuilder new
       
    18 		numRargs: 1;
       
    19 		addTemps: #(self);		"receiver and args declarations"
       
    20 		pushLiteral: 1;				
       
    21 		returnTop;
       
    22 		ir.
       
    23 
       
    24 	(iRMethod allSequences last) last delete.
       
    25 	(iRMethod allSequences last) last delete.
       
    26 
       
    27 	(iRMethod allSequences last)
       
    28 			add: (IRInstruction pushLiteral: 2).
       
    29 
       
    30 	(iRMethod allSequences last)
       
    31 			add: (IRInstruction returnTop).
       
    32 
       
    33 	aCompiledMethod := iRMethod compiledMethod.
       
    34 	self should: [(aCompiledMethod valueWithReceiver: nil arguments: #() ) = 2].
       
    35 !
       
    36 
       
    37 testAddBefore
       
    38 	
       
    39 	| iRMethod aCompiledMethod ret |
       
    40 
       
    41 	iRMethod := IRBuilder new
       
    42 		numRargs: 1;
       
    43 		addTemps: #(self);		"receiver and args declarations"
       
    44 		pushLiteral: 1;				
       
    45 		returnTop;
       
    46 		ir.
       
    47 
       
    48 	(iRMethod allSequences last) last delete.
       
    49 	(iRMethod allSequences last) last delete.
       
    50 
       
    51 	ret :=  (IRInstruction returnTop).
       
    52 
       
    53 	(iRMethod allSequences last)
       
    54 			add: ret.
       
    55 
       
    56 	(iRMethod allSequences last)
       
    57 			add: (IRInstruction pushLiteral: 2) before: ret.
       
    58 
       
    59 	aCompiledMethod := iRMethod compiledMethod.
       
    60 	self should: [(aCompiledMethod valueWithReceiver: nil arguments: #() ) = 2].
       
    61 !
       
    62 
       
    63 testAddIntructions
       
    64 	
       
    65 	| iRMethod aCompiledMethod |
       
    66 
       
    67 	iRMethod := IRBuilder new
       
    68 		numRargs: 1;
       
    69 		addTemps: #(self);		"receiver and args declarations"
       
    70 		pushLiteral: 1;				
       
    71 		returnTop;
       
    72 		ir.
       
    73 
       
    74 	(iRMethod allSequences last) last delete.
       
    75 	(iRMethod allSequences last) last delete.
       
    76 
       
    77 	(iRMethod allSequences last)
       
    78 			addInstructions: {(IRInstruction pushLiteral: 2). (IRInstruction returnTop)}.
       
    79 
       
    80 	aCompiledMethod := iRMethod compiledMethod.
       
    81 	self should: [(aCompiledMethod valueWithReceiver: nil arguments: #() ) = 2].
       
    82 !
       
    83 
       
    84 testAddIntructionsBefore
       
    85 	
       
    86 	| iRMethod aCompiledMethod push |
       
    87 
       
    88 	iRMethod := IRBuilder new
       
    89 		numRargs: 1;
       
    90 		addTemps: #(self);		"receiver and args declarations"
       
    91 		pushLiteral: 1;				
       
    92 		returnTop;
       
    93 		ir.
       
    94 
       
    95 	push := (iRMethod allSequences last) at: (iRMethod allSequences size - 1) .
       
    96 
       
    97 	(iRMethod allSequences last)
       
    98 			addInstructions: {(IRInstruction pushLiteral: 2). (IRInstruction returnTop)} before: push.
       
    99 
       
   100 	aCompiledMethod := iRMethod compiledMethod.
       
   101 	self should: [(aCompiledMethod valueWithReceiver: nil arguments: #() ) = 2].
       
   102 !
       
   103 
       
   104 testAddIntructionsBeforeFromLList
       
   105 	
       
   106 	| iRMethod aCompiledMethod push llist col |
       
   107 
       
   108 	iRMethod := IRBuilder new
       
   109 		numRargs: 1;
       
   110 		addTemps: #(self);		"receiver and args declarations"
       
   111 		pushLiteral: 1;				
       
   112 		returnTop;
       
   113 		ir.
       
   114 
       
   115 	push := (iRMethod allSequences last) at: (iRMethod allSequences size - 1) .
       
   116 
       
   117 	llist := LinkedList new.
       
   118 	llist add: (IRInstruction pushLiteral: 2).
       
   119 	llist add: (IRInstruction returnTop).
       
   120 
       
   121 	col := llist asOrderedCollection.
       
   122 
       
   123 	(iRMethod allSequences last)
       
   124 			addInstructions:  col before: push.
       
   125 
       
   126 	aCompiledMethod := iRMethod compiledMethod.
       
   127 	self should: [(aCompiledMethod valueWithReceiver: nil arguments: #() ) = 2].
       
   128 !
       
   129 
       
   130 testDelete
       
   131 	
       
   132 	| iRMethod aCompiledMethod |
       
   133 
       
   134 	iRMethod := IRBuilder new
       
   135 		numRargs: 1;
       
   136 		addTemps: #(self);		"receiver and args declarations"
       
   137 		pushLiteral: 1;				
       
   138 		pushLiteral: 2;	
       
   139 		returnTop;
       
   140 		ir.
       
   141 
       
   142 	((iRMethod allSequences last) 
       
   143 		detect: [:each | each isConstant: [:c | c == 2]]) delete.
       
   144 			
       
   145 
       
   146 	aCompiledMethod := iRMethod compiledMethod.
       
   147 	self should: [(aCompiledMethod valueWithReceiver: nil arguments: #() ) = 1].
       
   148 !
       
   149 
       
   150 testReplace
       
   151 	| iRMethod aCompiledMethod |
       
   152 	
       
   153 	iRMethod := IRBuilder new
       
   154 		numRargs: 1;
       
   155 		addTemps: #(self);		"receiver and args declarations"
       
   156 		pushLiteral: 1;				
       
   157 		returnTop;
       
   158 		ir.
       
   159 	
       
   160 	(iRMethod allSequences last at: 1) 
       
   161 			replaceWith: (IRInstruction pushLiteral: 2).
       
   162 
       
   163 	aCompiledMethod := iRMethod compiledMethod.
       
   164 	self should: [(aCompiledMethod valueWithReceiver: nil arguments: #() ) = 2].
       
   165 !
       
   166 
       
   167 testReplaceInstr
       
   168 	
       
   169 	| iRMethod aCompiledMethod |
       
   170 
       
   171 	iRMethod := IRBuilder new
       
   172 		numRargs: 1;
       
   173 		addTemps: #(self);		"receiver and args declarations"
       
   174 		pushLiteral: 1;				
       
   175 		returnTop;
       
   176 		ir.
       
   177 	
       
   178 	(iRMethod allSequences last at: 1) 
       
   179 			replaceWithInstructions: {(IRInstruction pushLiteral: 2)}.
       
   180 
       
   181 	aCompiledMethod := iRMethod compiledMethod.
       
   182 	self should: [(aCompiledMethod valueWithReceiver: nil arguments: #() ) = 2].
       
   183 ! !
       
   184 
       
   185 !IRTransformTest class methodsFor:'documentation'!
       
   186 
       
   187 version
       
   188     ^'$Id$'
       
   189 ! !