Skip to content

Commit

Permalink
Converted simple to safe API
Browse files Browse the repository at this point in the history
  • Loading branch information
endobson committed Sep 17, 2011
1 parent 4b32594 commit aec892a
Show file tree
Hide file tree
Showing 29 changed files with 688 additions and 38 deletions.
80 changes: 79 additions & 1 deletion private/ffi/arithmetic.rkt
Expand Up @@ -7,7 +7,42 @@

(require ffi/unsafe)

(provide (all-defined-out))
(provide
(except-out (all-defined-out)
safe:binop
safe:uniop
safe:icmp))

;TODO differentiate types and ensure that types match,
;and contexts match
(define safe:binop
(_fun (builder : safe:LLVMBuilderRef)
safe:LLVMValueRef
safe:LLVMValueRef
_non-null-string ->
(ptr : _pointer) ->
(safe:llvm-value-ref ptr (safe:llvm-builder-ref-module builder))))

(define safe:uniop
(_fun (builder : safe:LLVMBuilderRef)
safe:LLVMValueRef
_non-null-string ->
(ptr : _pointer) ->
(safe:llvm-value-ref ptr (safe:llvm-builder-ref-module builder))))

(define safe:icmp
(_fun (builder : safe:LLVMBuilderRef)
LLVMIntPredicate
safe:LLVMValueRef
safe:LLVMValueRef
_non-null-string ->
(ptr : _pointer) ->
(safe:llvm-value-ref ptr (safe:llvm-builder-ref-module builder))))






;/* Arithmetic */
(define-llvm-multiple-unsafe
Expand Down Expand Up @@ -38,6 +73,36 @@
LLVMBuildXor)
(_fun LLVMBuilderRef LLVMValueRef LLVMValueRef _string -> LLVMValueRef))


(define-llvm-multiple-safe
(LLVMBuildAdd
LLVMBuildNSWAdd
LLVMBuildNUWAdd
LLVMBuildFAdd
LLVMBuildSub
LLVMBuildNSWSub
LLVMBuildNUWSub
LLVMBuildFSub
LLVMBuildMul
LLVMBuildNSWMul
LLVMBuildNUWMul
LLVMBuildFMul
LLVMBuildUDiv
LLVMBuildSDiv
LLVMBuildExactSDiv
LLVMBuildFDiv
LLVMBuildURem
LLVMBuildSRem
LLVMBuildFRem
LLVMBuildShl
LLVMBuildLShr
LLVMBuildAShr
LLVMBuildAnd
LLVMBuildOr
LLVMBuildXor)
safe:binop)


(define-llvm-unsafe LLVMBuildBinOp
(_fun LLVMBuilderRef LLVMOpcode LLVMValueRef LLVMValueRef _string -> LLVMValueRef))

Expand All @@ -50,6 +115,16 @@
(_fun LLVMBuilderRef LLVMValueRef _string -> LLVMValueRef))


(define-llvm-multiple-safe
(LLVMBuildNeg
LLVMBuildNSWNeg
LLVMBuildNUWNeg
LLVMBuildFNeg
LLVMBuildNot)
safe:uniop)



;/* Comparisons */
(define-llvm-unsafe LLVMBuildICmp
(_fun LLVMBuilderRef
Expand All @@ -58,6 +133,9 @@
LLVMValueRef
_string -> LLVMValueRef))


(define-llvm-safe LLVMBuildICmp safe:icmp)

(define-llvm-unsafe LLVMBuildFCmp
(_fun LLVMBuilderRef
LLVMRealPredicate
Expand Down
15 changes: 15 additions & 0 deletions private/ffi/basic-blocks.rkt
Expand Up @@ -22,6 +22,12 @@
;(define-llvm LLVMGetBasicBlockTerminator (_fun LLVMBasicBlockRef -> LLVMValueRef))
;Not yet in my dev repo of llvm


(define-llvm-safe LLVMGetBasicBlockParent
(_fun (bb : safe:LLVMBasicBlockRef) ->
(ptr : _pointer) ->
(safe:llvm-value-ref ptr (safe:llvm-basic-block-ref-module bb))))

(define-llvm-unsafe LLVMCountBasicBlocks (_fun LLVMValueRef -> _uint))
(define-llvm-unsafe LLVMGetBasicBlocks
(_fun (fun) ::
Expand All @@ -44,6 +50,15 @@
(define-llvm-unsafe LLVMAppendBasicBlockInContext (_fun LLVMContextRef LLVMValueRef _string -> LLVMBasicBlockRef))
(define-llvm-unsafe LLVMInsertBasicBlockInContext (_fun LLVMContextRef LLVMBasicBlockRef _string -> LLVMBasicBlockRef))

;TODO check that function value's context and ctx are the same
;and that it is a function value
(define-llvm-safe LLVMAppendBasicBlockInContext
(_fun (ctx fun name) ::
(ctx : safe:LLVMContextRef)
(fun : safe:LLVMValueRef)
(name : _non-null-string) ->
(bb : _pointer) ->
(safe:llvm-basic-block-ref bb ctx)))

(define-llvm-unsafe LLVMAppendBasicBlock
(_fun LLVMValueRef _string -> LLVMBasicBlockRef))
Expand Down
23 changes: 22 additions & 1 deletion private/ffi/builder.rkt
Expand Up @@ -6,7 +6,20 @@

(require ffi/unsafe)

(provide (all-defined-out))
(provide (except-out (all-defined-out)
safe:position-builder-at-end))



(define safe:position-builder-at-end
(_fun (builder bb) ::
(builder : safe:LLVMBuilderRef)
(bb : safe:LLVMBasicBlockRef) ->
_void ->
(begin
(set-safe:llvm-builder-ref-module! builder
(safe:llvm-basic-block-ref-module bb))
(void))))

;/*===-- Instruction builders ----------------------------------------------===*/

Expand All @@ -17,13 +30,20 @@
(define-llvm-unsafe LLVMCreateBuilderInContext (_fun LLVMContextRef -> LLVMBuilderRef))
(define-llvm-unsafe LLVMCreateBuilder (_fun -> LLVMBuilderRef))

(define-llvm-safe LLVMCreateBuilderInContext safe:LLVMBuilderCreator)

(define-llvm-unsafe LLVMPositionBuilder (_fun LLVMBuilderRef LLVMBasicBlockRef LLVMValueRef -> _void))
(define-llvm-unsafe LLVMPositionBuilderBefore
(_fun LLVMBuilderRef LLVMValueRef -> _void))

(define-llvm-unsafe LLVMPositionBuilderAtEnd (_fun LLVMBuilderRef LLVMBasicBlockRef -> _void))
(define-llvm-safe LLVMPositionBuilderAtEnd safe:position-builder-at-end)

(define-llvm-unsafe LLVMGetInsertBlock (_fun LLVMBuilderRef -> LLVMBasicBlockRef))
(define-llvm-safe LLVMGetInsertBlock
(_fun (builder : safe:LLVMBuilderRef) ->
(ptr : _pointer) ->
(safe:llvm-basic-block-ref ptr (safe:llvm-builder-ref-module builder))))

(define-llvm-unsafe LLVMClearInsertionPosition
(_fun LLVMBuilderRef -> _void))
Expand All @@ -34,6 +54,7 @@
(_fun LLVMBuilderRef LLVMValueRef _string -> _void))

(define-llvm-unsafe LLVMDisposeBuilder (_fun LLVMBuilderRef -> _void))
(define (safe:LLVMDisposeBuilder builder) (void))



Expand Down
43 changes: 42 additions & 1 deletion private/ffi/cast.rkt
Expand Up @@ -7,7 +7,22 @@

(require ffi/unsafe)

(provide (all-defined-out))

;TODO differentiate types and ensure that types match,
;and contexts match
(define safe:icast
(_fun (builder : safe:LLVMBuilderRef)
safe:LLVMValueRef
safe:LLVMTypeRef
_non-null-string ->
(ptr : _pointer) ->
(safe:llvm-value-ref ptr (safe:llvm-builder-ref-module builder))))

(define safe:pcast safe:icast)



(provide (except-out (all-defined-out) safe:pcast safe:icast))

(define-llvm-multiple-unsafe
(LLVMBuildTrunc
Expand All @@ -33,5 +48,31 @@
LLVMBuildFPCast)
(_fun LLVMBuilderRef LLVMValueRef LLVMTypeRef _string -> LLVMValueRef))


(define-llvm-multiple-safe
(LLVMBuildTrunc
LLVMBuildZExt
LLVMBuildSExt
LLVMBuildFPToUI
LLVMBuildFPToSI
LLVMBuildUIToFP
LLVMBuildSIToFP
LLVMBuildFPTrunc
LLVMBuildFPExt)
safe:icast)

(define-llvm-multiple-safe
(LLVMBuildPtrToInt
LLVMBuildIntToPtr
LLVMBuildBitCast
LLVMBuildZExtOrBitCast
LLVMBuildSExtOrBitCast
LLVMBuildTruncOrBitCast
LLVMBuildPointerCast
LLVMBuildIntCast
LLVMBuildFPCast)
safe:pcast)


(define-llvm-unsafe LLVMBuildCast
(_fun LLVMBuilderRef LLVMOpcode LLVMValueRef LLVMTypeRef _string -> LLVMValueRef))
59 changes: 59 additions & 0 deletions private/ffi/constants.rkt
Expand Up @@ -19,6 +19,11 @@
-> LLVMTypeRef))


(define-llvm-safe LLVMConstInt
(_fun (ty : safe:LLVMTypeRef) _long LLVMBool ->
(ptr : _pointer) ->
(safe:llvm-value-ref ptr (safe:llvm-type-ref-context ty))))

;/* Operations on constants of any type */
(define-llvm-multiple-unsafe
(LLVMConstNull ; /* all zeroes */
Expand All @@ -28,6 +33,17 @@
(_fun LLVMTypeRef -> LLVMValueRef))


(define-llvm-multiple-safe
(LLVMConstNull ; /* all zeroes */
LLVMConstAllOnes ; /* only for int/vector */
LLVMGetUndef
LLVMConstPointerNull)
(_fun (ty : safe:LLVMTypeRef) ->
(ptr : _pointer) ->
(safe:llvm-value-ref ptr (safe:llvm-type-ref-context ty))))



(define-llvm-unsafe LLVMConstIntOfString
(_fun LLVMTypeRef _string _uint8 -> LLVMValueRef))
(define-llvm-unsafe LLVMConstIntOfStringAndSize
Expand All @@ -50,6 +66,17 @@
(_uint = (string-length str))
(dnt : LLVMBool)
-> LLVMValueRef))


(define-llvm-safe LLVMConstStringInContext
(_fun (context str dnt) ::
(context : safe:LLVMContextRef)
(str : _string)
(_uint = (string-length str))
(dnt : LLVMBool) ->
(ptr : _pointer) ->
(safe:llvm-value-ref ptr context)))



(define-llvm-unsafe LLVMConstStructInContext
Expand All @@ -59,6 +86,17 @@
(_uint = (length fields))
(packed : LLVMBool)
-> LLVMValueRef))


(define-llvm-safe LLVMConstStructInContext
(_fun (context fields packed) ::
(context : safe:LLVMContextRef)
(fields : (_list i safe:LLVMValueRef))
(_uint = (length fields))
(packed : LLVMBool) ->
(ptr : _pointer) ->
(safe:llvm-value-ref ptr context)))



(define-llvm-unsafe LLVMConstString
Expand All @@ -85,13 +123,34 @@
-> LLVMValueRef))


(define-llvm-safe LLVMConstNamedStruct
(_fun (type fields) ::
(type : safe:LLVMTypeRef)
(fields : (_list i safe:LLVMValueRef))
(_uint = (length fields)) ->
(ptr : _pointer) ->
(safe:llvm-value-ref ptr (safe:llvm-type-ref-context type))))



(define-llvm-unsafe LLVMConstArray
(_fun (type elements) ::
(type : LLVMTypeRef)
(elements : (_list i LLVMValueRef))
(_uint = (length elements))
-> LLVMValueRef))

(define-llvm-safe LLVMConstArray
(_fun (type elements) ::
(type : safe:LLVMTypeRef)
(elements : (_list i safe:LLVMValueRef))
(_uint = (length elements)) ->
(ptr : _pointer) ->
(safe:llvm-value-ref ptr (safe:llvm-type-ref-context type))))





(define-llvm-unsafe LLVMConstVector
(_fun (elements) ::
Expand Down

0 comments on commit aec892a

Please sign in to comment.