Initial support for literals
authorJan Vrany <jan.vrany@fit.cvut.cz>
Mon, 20 Jun 2016 09:38:10 +0100
changeset 28 4bdee0ee3d83
parent 27 73e7acbd16ff
child 29 5693302d4e24
Initial support for literals
c1/DragonFly__C1Compiler.st
c1/DragonFly__C1CompilerTests.st
c1/DragonFly__C1LLVMTypes.st
c1/Make.proto
c1/bc.mak
--- a/c1/DragonFly__C1Compiler.st	Fri Jun 17 23:50:59 2016 +0100
+++ b/c1/DragonFly__C1Compiler.st	Mon Jun 20 09:38:10 2016 +0100
@@ -17,7 +17,7 @@
 
 Object subclass:#C1Compiler
 	instanceVariableNames:'method module function asm prologue epilogue contextSetup
-		literals context stack'
+		literals literalsBaseAddr context stack'
 	classVariableNames:'SelectorSpecialCharMappingTable'
 	poolDictionaries:'DragonFly::C1LLVMTypes LLVMIntPredicate VMData VMOffsets
 		VMConstants'
@@ -161,12 +161,15 @@
      and update method's code pointer"
     | opts mm object jit literalsSection |
 
+    object := CompiledCodeObject forCompiledCode: method text: 0 literals: literals size ilcs: 0.
+    literals notEmptyOrNil ifTrue:[ 
+        literalsSection := object literals.
+        1 to: literals size do:[:i | 
+            literalsSection at: i put: (literals at: i).
+        ].
+        literalsBaseAddr initializer: (LLVMConstant pointer: literalsSection address type: TyOBJVec)
+    ].
     module verify.
-    object := CompiledCodeObject forCompiledCode: method text: 0 literals: literals size ilcs: 0.
-    literalsSection := object literals.
-    1 to: literals size do:[:i | 
-        literalsSection at: i put: (literals at: i).
-    ].
     mm := C1LLVMMCJITMemoryManager for: object.
     opts := LLVMMCJITCompilerOptions new.
     opts MCJMM: mm.
@@ -174,7 +177,7 @@
     method code: (jit addressOfFunction: function).
 
     "Created: / 21-04-2016 / 09:19:12 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-    "Modified: / 18-06-2016 / 22:27:40 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 20-06-2016 / 09:22:34 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 prepare
@@ -189,9 +192,10 @@
     1 to: method numArgs do:[:i | (function parameterAt: OBJFUNCArgIndexArgBase + i) name: 'arg' , i printString ].
     asm := function builder.
     literals := OrderedCollection with: method.
+    literalsBaseAddr := module addGlobalNamed: '__literals' value: (LLVMConstant null: TyOBJVec)
 
     "Created: / 21-04-2016 / 09:17:06 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-    "Modified: / 18-06-2016 / 22:26:21 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 20-06-2016 / 20:28:18 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !C1Compiler methodsFor:'private'!
@@ -263,6 +267,9 @@
     asm store:(self loadSmallInteger: flags)  
            at:(asm gep:context at:{ 0 . TyContextFieldIndexFlags }).
 
+     asm store:(self loadLiteral: method)  
+          at:(asm gep:context at:{ 0 . TyContextFieldIndexMethod }).      
+
     asm store:self loadReceiver
           at:(asm gep:context at:{ 0 . TyContextFieldIndexReceiver }).
 
@@ -278,7 +285,7 @@
     self storeThisContext: context.
 
     "Created: / 20-04-2016 / 23:12:22 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-    "Modified: / 18-06-2016 / 23:43:15 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 20-06-2016 / 20:44:18 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !C1Compiler methodsFor:'private-load / store'!
@@ -334,19 +341,40 @@
     "Created: / 18-06-2016 / 00:47:48 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
+loadLiteral: anObject
+    | index |
+    anObject class == SmallInteger ifTrue:[ 
+        ^ self loadSmallInteger: anObject.  
+    ].
+    anObject isNil ifTrue:[ 
+        ^ self loadNil.
+    ].
+    index := literals identityIndexOf: anObject.
+    index == 0 ifTrue:[
+        literals add: anObject.
+        index := literals size.
+    ].
+    ^ asm load: (asm gep: (asm load: literalsBaseAddr) at: index - 1)
+            as: ('literal_', index printString)
+
+    "Created: / 20-06-2016 / 20:41:30 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 20-06-2016 / 23:35:48 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
 loadNil
-    ^ asm int:(LLVMConstant uintptr:0) toPtr:TyOBJ.
+    ^ asm int:(LLVMConstant uintptr:0) toPtr:TyOBJ as: 'nil'
 
     "Created: / 21-04-2016 / 13:59:08 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 20-06-2016 / 23:36:50 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 loadReceiver
     ^ context isNil 
         ifTrue:[ function parameterAt: OBJFUNCArgIndexReceiver ]
-        ifFalse:[ asm load: (asm gep: context at:{ 0 . TyContextFieldIndexReceiver }) ]
+        ifFalse:[ asm load: (asm gep: context at:{ 0 . TyContextFieldIndexReceiver }) as: 'zelf' ]
 
     "Created: / 20-04-2016 / 22:09:34 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-    "Modified: / 20-04-2016 / 23:25:57 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 20-06-2016 / 23:37:09 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 loadRetvalTemp
@@ -359,25 +387,27 @@
 loadSearchClass
     ^ context isNil 
         ifTrue:[ function parameterAt: OBJFUNCArgIndexSearchClass ]
-        ifFalse:[ asm load: (asm gep: context at:{ 0 . TyContextFieldIndexSearchClass }) ]
+        ifFalse:[ asm load: (asm gep: context at:{ 0 . TyContextFieldIndexSearchClass }) as: 'searchClass' ]
 
     "Created: / 20-04-2016 / 23:27:03 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 20-06-2016 / 23:37:24 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 loadSelector
     ^ context isNil 
         ifTrue:[ function parameterAt: OBJFUNCArgIndexSelector ]
-        ifFalse:[ asm load: (asm gep: context at:{ 0 . TyContextFieldIndexSelector }) ]
+        ifFalse:[ asm load: (asm gep: context at:{ 0 . TyContextFieldIndexSelector }) as: 'selector']
 
     "Created: / 20-04-2016 / 23:24:12 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 20-06-2016 / 23:37:33 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 loadSmallInteger: value
     self assert: (value between: SmallInteger minVal and: SmallInteger maxVal).
-    ^asm int: (LLVMConstant sintptr: (value << 1) + 1) toPtr: TyOBJ
+    ^asm int: (LLVMConstant sintptr: (value << 1) + 1) toPtr: TyOBJ as:'smallint_', value printString
 
     "Created: / 18-06-2016 / 22:15:48 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-    "Modified: / 18-06-2016 / 23:30:24 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 20-06-2016 / 23:37:55 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 loadThisContext
--- a/c1/DragonFly__C1CompilerTests.st	Fri Jun 17 23:50:59 2016 +0100
+++ b/c1/DragonFly__C1CompilerTests.st	Mon Jun 20 09:38:10 2016 +0100
@@ -117,6 +117,30 @@
     "Created: / 12-02-2016 / 12:26:14 / Jan Vrany <jan.vrany@fit.cvut.cz>"
     "Modified: / 21-04-2016 / 09:23:57 / Jan Vrany <jan.vrany@fit.cvut.cz>"
     "Modified (format): / 21-04-2016 / 14:02:06 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+test_loadLiteral
+    | cookie method compiler asm |
+
+    cookie := Object new.
+    method := Method new.
+    method numberOfArgs: 0.
+    method numberOfVars: 0.
+    method stackSize: 0.
+
+    compiler := C1Compiler new.
+    compiler method: method.
+    compiler prepare.
+
+    asm := compiler instVarNamed: #asm.
+    asm ret: (compiler loadLiteral: cookie).
+
+    compiler finish.
+
+    self assert: method code notNil.
+    self assert: (method valueWithReceiver:nil arguments: #()) == cookie.
+
+    "Created: / 20-06-2016 / 23:40:58 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !C1CompilerTests methodsFor:'tests - codegen'!
--- a/c1/DragonFly__C1LLVMTypes.st	Fri Jun 17 23:50:59 2016 +0100
+++ b/c1/DragonFly__C1LLVMTypes.st	Mon Jun 20 09:38:10 2016 +0100
@@ -20,7 +20,7 @@
 	classVariableNames:'TyInstance TyInstanceFields TyInstanceFieldIndexClass
 		TyInstanceFieldIndexSize TyInstanceFieldIndexSpace
 		TyInstanceFieldIndexFlags TyInstanceFieldIndexAge
-		TyInstanceFieldIndexHashLow TyOBJ TyInlineCache
+		TyInstanceFieldIndexHashLow TyOBJ TyOBJVec TyInlineCache
 		TyInlineCacheIndexFunc TyInlineCacheIndexClass
 		TyInlineCacheIndexLineNo TyInlineCachePtr TyOBJFUNC TyOBJFUNCs
 		TyContextFields TyContextFieldIndexStack TyContexts
@@ -62,6 +62,7 @@
 
     TyInstance := LLVMType named: '__instance'.
     TyOBJ := TyInstance pointer.
+    TyOBJVec := TyOBJ pointer.
     TyInstanceFields := {
         TyOBJ.                      "/ o_class
         LLVMType int32.             "/ o_size
@@ -246,7 +247,7 @@
 
     TyMKREALCONTEXT5 := LLVMType function: { TyOBJ } returning: TyOBJ
 
-    "Modified: / 17-06-2016 / 23:21:28 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 19-06-2016 / 09:45:21 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !C1LLVMTypes class methodsFor:'accessing'!
--- a/c1/Make.proto	Fri Jun 17 23:50:59 2016 +0100
+++ b/c1/Make.proto	Mon Jun 20 09:38:10 2016 +0100
@@ -126,7 +126,7 @@
 $(OUTDIR)DragonFly__C1LLVMMCJITMemoryManager.$(O) DragonFly__C1LLVMMCJITMemoryManager.$(C) DragonFly__C1LLVMMCJITMemoryManager.$(H): DragonFly__C1LLVMMCJITMemoryManager.st $(INCLUDE_TOP)/jv/llvm_s/LLVMDisposableObject.$(H) $(INCLUDE_TOP)/jv/llvm_s/LLVMMCJITMemoryManager.$(H) $(INCLUDE_TOP)/jv/llvm_s/LLVMObject.$(H) $(INCLUDE_TOP)/stx/libbasic/ExternalAddress.$(H) $(INCLUDE_TOP)/stx/libbasic/Object.$(H) $(STCHDR)
 $(OUTDIR)DragonFly__C1LLVMTypes.$(O) DragonFly__C1LLVMTypes.$(C) DragonFly__C1LLVMTypes.$(H): DragonFly__C1LLVMTypes.st $(INCLUDE_TOP)/stx/libbasic/Object.$(H) $(INCLUDE_TOP)/stx/libbasic/SharedPool.$(H) $(STCHDR)
 $(OUTDIR)jv_dragonfly_c1.$(O) jv_dragonfly_c1.$(C) jv_dragonfly_c1.$(H): jv_dragonfly_c1.st $(INCLUDE_TOP)/stx/libbasic/LibraryDefinition.$(H) $(INCLUDE_TOP)/stx/libbasic/Object.$(H) $(INCLUDE_TOP)/stx/libbasic/ProjectDefinition.$(H) $(STCHDR)
-$(OUTDIR)DragonFly__C1Compiler.$(O) DragonFly__C1Compiler.$(C) DragonFly__C1Compiler.$(H): DragonFly__C1Compiler.st $(INCLUDE_TOP)/jv/dragonfly/VMData.$(H) $(INCLUDE_TOP)/jv/dragonfly/VMOffsets.$(H) $(INCLUDE_TOP)/jv/dragonfly/c1/DragonFly__C1LLVMTypes.$(H) $(INCLUDE_TOP)/jv/llvm_s/LLVMIntPredicate.$(H) $(INCLUDE_TOP)/stx/libbasic/Object.$(H) $(STCHDR)
+$(OUTDIR)DragonFly__C1Compiler.$(O) DragonFly__C1Compiler.$(C) DragonFly__C1Compiler.$(H): DragonFly__C1Compiler.st $(INCLUDE_TOP)/jv/dragonfly/VMConstants.$(H) $(INCLUDE_TOP)/jv/dragonfly/VMData.$(H) $(INCLUDE_TOP)/jv/dragonfly/VMOffsets.$(H) $(INCLUDE_TOP)/jv/dragonfly/c1/DragonFly__C1LLVMTypes.$(H) $(INCLUDE_TOP)/jv/llvm_s/LLVMIntPredicate.$(H) $(INCLUDE_TOP)/stx/libbasic/Object.$(H) $(STCHDR)
 
 # ENDMAKEDEPEND --- do not remove this line
 
--- a/c1/bc.mak	Fri Jun 17 23:50:59 2016 +0100
+++ b/c1/bc.mak	Mon Jun 20 09:38:10 2016 +0100
@@ -73,7 +73,7 @@
 $(OUTDIR)DragonFly__C1LLVMMCJITMemoryManager.$(O) DragonFly__C1LLVMMCJITMemoryManager.$(C) DragonFly__C1LLVMMCJITMemoryManager.$(H): DragonFly__C1LLVMMCJITMemoryManager.st $(INCLUDE_TOP)\jv\llvm_s\LLVMDisposableObject.$(H) $(INCLUDE_TOP)\jv\llvm_s\LLVMMCJITMemoryManager.$(H) $(INCLUDE_TOP)\jv\llvm_s\LLVMObject.$(H) $(INCLUDE_TOP)\stx\libbasic\ExternalAddress.$(H) $(INCLUDE_TOP)\stx\libbasic\Object.$(H) $(STCHDR)
 $(OUTDIR)DragonFly__C1LLVMTypes.$(O) DragonFly__C1LLVMTypes.$(C) DragonFly__C1LLVMTypes.$(H): DragonFly__C1LLVMTypes.st $(INCLUDE_TOP)\stx\libbasic\Object.$(H) $(INCLUDE_TOP)\stx\libbasic\SharedPool.$(H) $(STCHDR)
 $(OUTDIR)jv_dragonfly_c1.$(O) jv_dragonfly_c1.$(C) jv_dragonfly_c1.$(H): jv_dragonfly_c1.st $(INCLUDE_TOP)\stx\libbasic\LibraryDefinition.$(H) $(INCLUDE_TOP)\stx\libbasic\Object.$(H) $(INCLUDE_TOP)\stx\libbasic\ProjectDefinition.$(H) $(STCHDR)
-$(OUTDIR)DragonFly__C1Compiler.$(O) DragonFly__C1Compiler.$(C) DragonFly__C1Compiler.$(H): DragonFly__C1Compiler.st $(INCLUDE_TOP)\jv\dragonfly\VMData.$(H) $(INCLUDE_TOP)\jv\dragonfly\VMOffsets.$(H) $(INCLUDE_TOP)\jv\dragonfly\c1\DragonFly__C1LLVMTypes.$(H) $(INCLUDE_TOP)\jv\llvm_s\LLVMIntPredicate.$(H) $(INCLUDE_TOP)\stx\libbasic\Object.$(H) $(STCHDR)
+$(OUTDIR)DragonFly__C1Compiler.$(O) DragonFly__C1Compiler.$(C) DragonFly__C1Compiler.$(H): DragonFly__C1Compiler.st $(INCLUDE_TOP)\jv\dragonfly\VMConstants.$(H) $(INCLUDE_TOP)\jv\dragonfly\VMData.$(H) $(INCLUDE_TOP)\jv\dragonfly\VMOffsets.$(H) $(INCLUDE_TOP)\jv\dragonfly\c1\DragonFly__C1LLVMTypes.$(H) $(INCLUDE_TOP)\jv\llvm_s\LLVMIntPredicate.$(H) $(INCLUDE_TOP)\stx\libbasic\Object.$(H) $(STCHDR)
 
 # ENDMAKEDEPEND --- do not remove this line