LLVMDIBuilder.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Wed, 06 Jul 2016 09:53:56 +0100
changeset 70 ced2a5c16e70
parent 55 41b3437f1fc7
permissions -rw-r--r--
Added builder support for llvm.memset , llvm.memmove and llvm.memcpy intrinsics For those, use specialized IR builder routines rather than manually calling the intrinsic. The problem which the latter is that those instrinsics are overloaded and - for reason not yet known - retrieving them cause crash. So better strick to LLVM way of doing so - through IRBuilder.

"
    Copyright (C) 2015-now Jan Vrany

    This code is not an open-source (yet). You may use this code
    for your own experiments and projects, given that:

    * all modification to the code will be sent to the
      original author for inclusion in future releases
    * this is not used in any commercial software

    This license is provisional and may (will) change in
    a future.
"
"{ Package: 'jv:llvm_s' }"

"{ NameSpace: Smalltalk }"

LLVMDisposableObject subclass:#LLVMDIBuilder
	instanceVariableNames:''
	classVariableNames:''
	poolDictionaries:'LLVMModuleFlagBehavior'
	category:'LLVM-S-Core'
!

!LLVMDIBuilder class methodsFor:'documentation'!

copyright
"
    Copyright (C) 2015-now Jan Vrany

    This code is not an open-source (yet). You may use this code
    for your own experiments and projects, given that:

    * all modification to the code will be sent to the
      original author for inclusion in future releases
    * this is not used in any commercial software

    This license is provisional and may (will) change in
    a future.
"
! !

!LLVMDIBuilder class methodsFor:'instance creation'!

new
    self shouldNotImplement. "/ use newForModule: instead

    "Created: / 05-10-2015 / 09:29:45 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

newForModule: module
    | dib |
    dib := LLVMCEXT NewDIBuilder: module.
    "/ Now we have to add module flags otherwise llc won't
    "/ produce debug info.
    "/ See http://lists.llvm.org/pipermail/llvm-dev/2014-October/077815.html
    module addFlag: 'Dwarf Version' value: (LLVMConstant uint32: 4) asLLVMMetadata behavior: LLVMModuleFlagBehaviorWarning.  
    module addFlag: 'Debug Info Version' value: (LLVMConstant uint32: 3) asLLVMMetadata behavior: LLVMModuleFlagBehaviorError.  

    ^ dib

    "Created: / 13-08-2015 / 06:35:14 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 15-08-2015 / 23:39:25 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!LLVMDIBuilder methodsFor:'assertions'!

assertIsStringOrFilename: value
    <resource: #skipInDebuggersWalkback>
    self assert: (value isString or:[ value isFilename ]) message: 'value is not a String or Filename'

    "Created: / 13-08-2015 / 06:47:54 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!LLVMDIBuilder methodsFor:'building'!

createAutomaticVariable: name in: scope file: file line: line type: type
    ^ self createAutomaticVariable: name in: scope file: file line: line type: type flags: 0

    "Created: / 14-08-2015 / 13:58:47 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

createAutomaticVariable: name in: scope file: file line: line type: type flags: flags
    ^ self createAutomaticVariable: name in: scope file: file line: line type: type flags: flags preserve: true.

    "Created: / 14-08-2015 / 13:57:04 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

createAutomaticVariable: name in: scope file: file line: line type: type flags: flags preserve: preserve
    "Creates a debg info metadata describing a local variable"

    self assertIsString: name.
    self assertIsDILocalScope: scope.  
    self assertIsDIFile: file.
    self assertIsIntegerUnsigned: line.
    self assertIsDIType: type.
    self assertIsIntegerUnsigned: flags.
    self assertIsBoolean: preserve.

    ^ LLVMCEXT DIBuilderCreateAutoVariable: self _: scope _: name _: file _: line _: type _: (preserve ifTrue:[1] ifFalse:[0]) _: flags

    "Created: / 14-08-2015 / 13:56:31 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 17-09-2015 / 17:00:50 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

createCompilationUnit: path language: lang producer: producer
    ^ self createCompilationUnit: path language: lang producer: producer runtimeVersion: 0 optimized: false flags: ''

    "Created: / 13-08-2015 / 06:58:35 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

createCompilationUnit: path language: lang producer: producer runtimeVersion: rv optimized: isOptimized flags: compilerFlags
    | filename |
    self assertIsStringOrFilename: path.
    self assertIsIntegerUnsigned: lang.
    self assertIsString: producer.
    self assertIsIntegerUnsigned: rv.
    self assertIsBoolean: isOptimized.
    self assertIsString: compilerFlags.

    filename := path asFilename.

    ^ LLVMCEXT DIBuilderCreateCompileUnit: self _: lang _: filename baseName _: filename directory pathName _: producer _: (isOptimized ifTrue:[1] ifFalse:[0]) _: compilerFlags _: rv

    "Created: / 13-08-2015 / 06:47:54 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

createExpression
    ^ LLVMCEXT DIBuilderCreateExpression: self _: ExternalAddress new _: 0

    "Created: / 14-08-2015 / 14:06:07 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

createFile: path
    | filename |

    self assertIsStringOrFilename: path.    

    filename := path asFilename.
    ^ LLVMCEXT DIBuilderCreateFile: self _: filename baseName _: filename directory pathName

    "Created: / 15-08-2015 / 22:08:35 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

createFunction: name in: scope file: file  line: line type: type local: isLocal definition: isDefinition optimized: isOptimized
    ^ self createFunction: name in: scope file: file  line: line type: type local: isLocal definition: isDefinition optimized: isOptimized flags: 0

    "Created: / 07-02-2016 / 07:59:39 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

createFunction: name in: scope file: file  line: line type: type local: isLocal definition: isDefinition optimized: isOptimized flags: flags
    ^ self createFunction: name in: scope file: file  line: line type: type local: isLocal definition: isDefinition optimized: isOptimized flags: flags scopeLine: line

    "Created: / 07-02-2016 / 07:59:10 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

createFunction: name in: scope file: file  line: line type: type local: isLocal definition: isDefinition optimized: isOptimized flags: flags scopeLine: scopeLine
    ^ self createFunction: name in: scope file: file  line: line type: type local: isLocal definition: isDefinition optimized: isOptimized flags: flags scopeLine: scopeLine linkageName: name

    "Created: / 07-02-2016 / 07:58:40 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

createFunction: name in: scope file: file  line: line type: type local: isLocal definition: isDefinition optimized: isOptimized flags: flags scopeLine: scopeLine linkageName: linkageName

    self assertIsDIScope: scope.          
    self assertIsDIFile: file.
    self assertIsString: name.
    self assertIsIntegerUnsigned: line.
    self assertIsBoolean: isLocal.
    self assertIsBoolean: isDefinition.
    self assertIsBoolean: isOptimized.
    self assertIsIntegerUnsigned: flags.
    self assertIsIntegerUnsigned: scopeLine.
    self assertIsString: linkageName.

    ^ LLVMCEXT DIBuilderCreateFunction: self  _: scope _: name _: linkageName _: file _: line _: type _: (isLocal ifTrue:[1] ifFalse:[0]) _: (isDefinition ifTrue:[1] ifFalse:[0]) _: scopeLine _: flags _: (isOptimized ifTrue:[1] ifFalse:[0])

    "Created: / 07-02-2016 / 07:57:51 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

createParameterVariable: name in: scope file: file line: line type: type flags: flags index: index
    ^  self createParameterVariable: name in: scope file: file line: line type: type flags: flags index: index preserve: true

    "Created: / 14-08-2015 / 13:54:20 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

createParameterVariable: name in: scope file: file line: line type: type flags: flags index: index  preserve: preserve
    "Creates a debg info metadata describing a local variable"

    self assertIsString: name.
    self assertIsDILocalScope: scope.  
    self assertIsDIFile: file.
    self assertIsIntegerUnsigned: line.
    self assertIsDIType: type.
    self assertIsIntegerUnsigned: flags.
    self assertIsIntegerUnsigned: index.
    self assertIsBoolean: preserve.

    ^ LLVMCEXT DIBuilderCreateParameterVariable: self _: scope _: name _: file _: line _: type _: (preserve ifTrue:[1] ifFalse:[0]) _: flags _: index

    "Created: / 14-08-2015 / 13:54:01 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 17-09-2015 / 17:13:42 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

createTypeFunction: parameterTypes
    | parameterTypesAsTuple |

    self assertIsDITypeArray: parameterTypes.

    parameterTypesAsTuple := LLVMCEXT MDNode2: LLVM GetGlobalContext _: parameterTypes asLLVMObjectArray _: parameterTypes size.
    ^ LLVMCEXT DIBuilderCreateSubroutineType: self _: parameterTypesAsTuple

    "Created: / 06-02-2016 / 15:25:17 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

createTypeScalar: name size: sizeInBits alignment: alignmentInBits encoding: encoding
    self assertIsString: name.
    self assertIsInteger64Unsigned: sizeInBits.
    self assertIsInteger64Unsigned: alignmentInBits.
    self assertIsIntegerUnsigned: alignmentInBits.

    ^ LLVMCEXT DIBuilderCreateBasicType: self _: name _: sizeInBits _: alignmentInBits _: encoding

    "Created: / 14-08-2015 / 07:32:18 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

createTypeScalar: name type: type encoding: encoding
    self assertIsString: name.
    self assertIsType: type.

    ^ self createTypeScalar: name size: type sizeInBits alignment:type alignmentInBits encoding: encoding

    "Created: / 14-08-2015 / 09:17:18 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

finish
    LLVMCEXT DIBuilderFinalize: self

    "Created: / 14-08-2015 / 13:11:35 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

insertDeclare: alloca variable: variable expression: expr location: location atEndOf: basicblock
    "Inserts @llvm.dbg.declare() intrinsic at the end of given `basicblock`"

    self assertIsValue: alloca.
    self assertIsDILocalVariable: variable.
    self assertIsDIExpression: expr.
    self assertIsDILocation: location.
    self assertIsBasicBlock: basicblock.

    ^ LLVMCEXT DIBuilderInsertDeclareAtEnd: self  _: alloca _: variable _: expr _: location _: basicblock

    "Created: / 15-08-2015 / 23:44:38 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 17-09-2015 / 17:03:04 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

insertValue: alloca variable: variable expression: expr location: location atEndOf: basicblock offset: offset
    "Inserts @llvm.dbg.value() intrinsic at the end of given `basicblock`"

    self assertIsValue: alloca.
    self assertIsDILocalVariable: variable.
    self assertIsDIExpression: expr.
    self assertIsDILocation: location.
    self assertIsBasicBlock: basicblock.
    self assertIsInteger64Unsigned: offset.  

    ^ LLVMCEXT DIBuilderInsertValueAtEnd: self _: alloca _: offset _: variable _: expr _: location _: basicblock

    "Created: / 15-08-2015 / 23:49:42 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 17-09-2015 / 17:03:30 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!LLVMDIBuilder methodsFor:'initialization & release'!

dispose
    "superclass LLVMDisposableObject says that I am responsible to implement this method"

    ^ LLVMCEXT DIBuilderDestroy: self

    "Modified: / 14-08-2015 / 13:20:32 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!LLVMDIBuilder class methodsFor:'documentation'!

version_HG

    ^ '$Changeset: <not expanded> $'
! !