11import  core:: { vec,  str,  option} ; 
22import str:: sbuf; 
33
4- import  llvm:: { TypeRef ,  MemoryBufferRef , 
5-               PassManagerRef ,  TargetDataRef , 
6-               ObjectFileRef ,  SectionIteratorRef } ; 
74import  ctypes:: { c_int,  c_uint,  unsigned,  longlong,  ulonglong} ; 
85
9- type  Long  = i32 ; 
10- type  Bool  = int ; 
6+ type  Opcode  = u32 ; 
7+ type  Bool  = unsigned ; 
8+ const  True :  Bool  = 1u32 ; 
9+ const  False :  Bool  = 0u32 ; 
1110
11+ // Consts for the LLVM CallConv type, pre-cast to uint. 
1212
13- const  True :  Bool  = 1 ; 
14- const  False :  Bool  = 0 ; 
13+ enum  CallConv  { 
14+     CCallConv  = 0 , 
15+     FastCallConv  = 8 , 
16+     ColdCallConv  = 9 , 
17+     X86StdcallCallConv  = 64 , 
18+     X86FastcallCallConv  = 65 , 
19+ } 
1520
16- // Consts for the LLVM CallConv type, pre-cast to uint. 
17- // FIXME: figure out a way to merge these with the native 
18- // typedef and/or a enum type in the native module below. 
19- 
20- const  LLVMCCallConv :  uint  = 0 u; 
21- const  LLVMFastCallConv :  uint  = 8 u; 
22- const  LLVMColdCallConv :  uint  = 9 u; 
23- const  LLVMX86StdcallCallConv :  uint  = 64 u; 
24- const  LLVMX86FastcallCallConv :  uint  = 65 u; 
25- 
26- const  LLVMDefaultVisibility :  uint  = 0 u; 
27- const  LLVMHiddenVisibility :  uint  = 1 u; 
28- const  LLVMProtectedVisibility :  uint  = 2 u; 
29- 
30- const  LLVMExternalLinkage :  uint  = 0 u; 
31- const  LLVMAvailableExternallyLinkage :  uint  = 1 u; 
32- const  LLVMLinkOnceAnyLinkage :  uint  = 2 u; 
33- const  LLVMLinkOnceODRLinkage :  uint  = 3 u; 
34- const  LLVMWeakAnyLinkage :  uint  = 4 u; 
35- const  LLVMWeakODRLinkage :  uint  = 5 u; 
36- const  LLVMAppendingLinkage :  uint  = 6 u; 
37- const  LLVMInternalLinkage :  uint  = 7 u; 
38- const  LLVMPrivateLinkage :  uint  = 8 u; 
39- const  LLVMDLLImportLinkage :  uint  = 9 u; 
40- const  LLVMDLLExportLinkage :  uint  = 10 u; 
41- const  LLVMExternalWeakLinkage :  uint  = 11 u; 
42- const  LLVMGhostLinkage :  uint  = 12 u; 
43- const  LLVMCommonLinkage :  uint  = 13 u; 
44- const  LLVMLinkerPrivateLinkage :  uint  = 14 u; 
45- const  LLVMLinkerPrivateWeakLinkage :  uint  = 15 u; 
46- const  LLVMLinkerPrivateWeakDefAutoLinkage :  uint  = 16 u; 
47- 
48- const  LLVMZExtAttribute :  uint  = 1 u; 
49- const  LLVMSExtAttribute :  uint  = 2 u; 
50- const  LLVMNoReturnAttribute :  uint  = 4 u; 
51- const  LLVMInRegAttribute :  uint  = 8 u; 
52- const  LLVMStructRetAttribute :  uint  = 16 u; 
53- const  LLVMNoUnwindAttribute :  uint  = 32 u; 
54- const  LLVMNoAliasAttribute :  uint  = 64 u; 
55- const  LLVMByValAttribute :  uint  = 128 u; 
56- const  LLVMNestAttribute :  uint  = 256 u; 
57- const  LLVMReadNoneAttribute :  uint  = 512 u; 
58- const  LLVMReadOnlyAttribute :  uint  = 1024 u; 
59- const  LLVMNoInlineAttribute :  uint  = 2048 u; 
60- const  LLVMAlwaysInlineAttribute :  uint  = 4096 u; 
61- const  LLVMOptimizeForSizeAttribute :  uint  = 8192 u; 
62- const  LLVMStackProtectAttribute :  uint  = 16384 u; 
63- const  LLVMStackProtectReqAttribute :  uint  = 32768 u; 
64- // 31 << 16 
65- const  LLVMAlignmentAttribute :  uint  = 2031616 u; 
66- const  LLVMNoCaptureAttribute :  uint  = 2097152 u; 
67- const  LLVMNoRedZoneAttribute :  uint  = 4194304 u; 
68- const  LLVMNoImplicitFloatAttribute :  uint  = 8388608 u; 
69- const  LLVMNakedAttribute :  uint  = 16777216 u; 
70- const  LLVMInlineHintAttribute :  uint  = 33554432 u; 
71- // 7 << 26 
72- const  LLVMStackAttribute :  uint  = 469762048 u; 
73- const  LLVMReturnsTwiceAttribute :  uint  = 536870912 u; 
74- // 1 << 30 
75- const  LLVMUWTableAttribute :  uint  = 1073741824 u; 
76- const  LLVMNonLazyBindAttribute :  uint  = 2147483648 u; 
21+ enum  Visibility  { 
22+     LLVMDefaultVisibility  = 0 , 
23+     HiddenVisibility  = 1 , 
24+     ProtectedVisibility  = 2 , 
25+ } 
26+ 
27+ enum  Linkage  { 
28+     ExternalLinkage  = 0 , 
29+     AvailableExternallyLinkage  = 1 , 
30+     LinkOnceAnyLinkage  = 2 , 
31+     LinkOnceODRLinkage  = 3 , 
32+     WeakAnyLinkage  = 4 , 
33+     WeakODRLinkage  = 5 , 
34+     AppendingLinkage  = 6 , 
35+     InternalLinkage  = 7 , 
36+     PrivateLinkage  = 8 , 
37+     DLLImportLinkage  = 9 , 
38+     DLLExportLinkage  = 10 , 
39+     ExternalWeakLinkage  = 11 , 
40+     GhostLinkage  = 12 , 
41+     CommonLinkage  = 13 , 
42+     LinkerPrivateLinkage  = 14 , 
43+     LinkerPrivateWeakLinkage  = 15 , 
44+     LinkerPrivateWeakDefAutoLinkage  = 16 , 
45+ } 
7746
47+ enum  Attribute  { 
48+     ZExtAttribute  = 1 , 
49+     SExtAttribute  = 2 , 
50+     NoReturnAttribute  = 4 , 
51+     InRegAttribute  = 8 , 
52+     StructRetAttribute  = 16 , 
53+     NoUnwindAttribute  = 32 , 
54+     NoAliasAttribute  = 64 , 
55+     ByValAttribute  = 128 , 
56+     NestAttribute  = 256 , 
57+     ReadNoneAttribute  = 512 , 
58+     ReadOnlyAttribute  = 1024 , 
59+     NoInlineAttribute  = 2048 , 
60+     AlwaysInlineAttribute  = 4096 , 
61+     OptimizeForSizeAttribute  = 8192 , 
62+     StackProtectAttribute  = 16384 , 
63+     StackProtectReqAttribute  = 32768 , 
64+     // 31 << 16 
65+     AlignmentAttribute  = 2031616 , 
66+     NoCaptureAttribute  = 2097152 , 
67+     NoRedZoneAttribute  = 4194304 , 
68+     NoImplicitFloatAttribute  = 8388608 , 
69+     NakedAttribute  = 16777216 , 
70+     InlineHintAttribute  = 33554432 , 
71+     // 7 << 26 
72+     StackAttribute  = 469762048 , 
73+     ReturnsTwiceAttribute  = 536870912 , 
74+     // 1 << 30 
75+     UWTableAttribute  = 1073741824 , 
76+     NonLazyBindAttribute  = 2147483648 , 
77+ } 
7878
7979// Consts for the LLVM IntPredicate type, pre-cast to uint. 
8080// FIXME: as above. 
8181
82- 
83- const   LLVMIntEQ :   uint  =  32 u ; 
84- const   LLVMIntNE :   uint  =  33 u ; 
85- const   LLVMIntUGT :   uint  =  34 u ; 
86- const   LLVMIntUGE :   uint  =  35 u ; 
87- const   LLVMIntULT :   uint  =  36 u ; 
88- const   LLVMIntULE :   uint  =  37 u ; 
89- const   LLVMIntSGT :   uint  =  38 u ; 
90- const   LLVMIntSGE :   uint  =  39 u ; 
91- const   LLVMIntSLT :   uint  =  40 u ; 
92- const   LLVMIntSLE :   uint  =  41 u ; 
93- 
82+ enum   IntPredicate   { 
83+      IntEQ  =  32 , 
84+      IntNE  =  33 , 
85+      IntUGT  =  34 , 
86+      IntUGE  =  35 , 
87+      IntULT  =  36 , 
88+      IntULE  =  37 , 
89+      IntSGT  =  38 , 
90+      IntSGE  =  39 , 
91+      IntSLT  =  40 , 
92+      IntSLE  =  41 , 
93+ } 
9494
9595// Consts for the LLVM RealPredicate type, pre-case to uint. 
9696// FIXME: as above. 
9797
98- const  LLVMRealOEQ :  uint  = 1 u; 
99- const  LLVMRealOGT :  uint  = 2 u; 
100- const  LLVMRealOGE :  uint  = 3 u; 
101- const  LLVMRealOLT :  uint  = 4 u; 
102- const  LLVMRealOLE :  uint  = 5 u; 
103- const  LLVMRealONE :  uint  = 6 u; 
104- 
105- const  LLVMRealORD :  uint  = 7 u; 
106- const  LLVMRealUNO :  uint  = 8 u; 
107- const  LLVMRealUEQ :  uint  = 9 u; 
108- const  LLVMRealUGT :  uint  = 10 u; 
109- const  LLVMRealUGE :  uint  = 11 u; 
110- const  LLVMRealULT :  uint  = 12 u; 
111- const  LLVMRealULE :  uint  = 13 u; 
112- const  LLVMRealUNE :  uint  = 14 u; 
98+ enum  RealPredicate  { 
99+     RealOEQ  = 1 , 
100+     RealOGT  = 2 , 
101+     RealOGE  = 3 , 
102+     RealOLT  = 4 , 
103+     RealOLE  = 5 , 
104+     RealONE  = 6 , 
105+     RealORD  = 7 , 
106+     RealUNO  = 8 , 
107+     RealUEQ  = 9 , 
108+     RealUGT  = 10 , 
109+     RealUGE  = 11 , 
110+     RealULT  = 12 , 
111+     RealULE  = 13 , 
112+     RealUNE  = 14 , 
113+ } 
114+ 
115+ // Opaque pointer types 
116+ enum  Module_opaque  { } 
117+ type  ModuleRef  = * Module_opaque ; 
118+ enum  Context_opaque  { } 
119+ type  ContextRef  = * Context_opaque ; 
120+ enum  Type_opaque  { } 
121+ type  TypeRef  = * Type_opaque ; 
122+ enum  Value_opaque  { } 
123+ type  ValueRef  = * Value_opaque ; 
124+ enum  BasicBlock_opaque  { } 
125+ type  BasicBlockRef  = * BasicBlock_opaque ; 
126+ enum  Builder_opaque  { } 
127+ type  BuilderRef  = * Builder_opaque ; 
128+ enum  MemoryBuffer_opaque  { } 
129+ type  MemoryBufferRef  = * MemoryBuffer_opaque ; 
130+ enum  PassManager_opaque  { } 
131+ type  PassManagerRef  = * PassManager_opaque ; 
132+ enum  PassManagerBuilder_opaque  { } 
133+ type  PassManagerBuilderRef  = * PassManagerBuilder_opaque ; 
134+ enum  Use_opaque  { } 
135+ type  UseRef  = * Use_opaque ; 
136+ enum  TargetData_opaque  { } 
137+ type  TargetDataRef  = * TargetData_opaque ; 
138+ enum  ObjectFile_opaque  { } 
139+ type  ObjectFileRef  = * ObjectFile_opaque ; 
140+ enum  SectionIterator_opaque  { } 
141+ type  SectionIteratorRef  = * SectionIterator_opaque ; 
113142
114143#[ link_args = "-Lrustllvm" ]  
115144#[ link_name = "rustllvm" ]  
116145#[ abi = "cdecl" ]  
117146nativemod  llvm { 
118- 
119-     type  ModuleRef ; 
120-     type  ContextRef ; 
121-     type  TypeRef ; 
122-     type  TypeHandleRef ; 
123-     type  ValueRef ; 
124-     type  BasicBlockRef ; 
125-     type  BuilderRef ; 
126-     type  ModuleProviderRef ; 
127-     type  MemoryBufferRef ; 
128-     type  PassManagerRef ; 
129-     type  PassManagerBuilderRef ; 
130-     type  UseRef ; 
131-     type  TargetDataRef ; 
132- 
133-     /* FIXME: These are enums in the C header. Represent them how, in rust? */ 
134-     type  Linkage ; 
135-     type  Attribute ; 
136-     type  Visibility ; 
137-     type  CallConv ; 
138-     type  IntPredicate ; 
139-     type  RealPredicate ; 
140-     type  Opcode ; 
141- 
142147    /* Create and destroy contexts. */ 
143148    fn  LLVMContextCreate ( )  -> ContextRef ; 
144149    fn  LLVMGetGlobalContext ( )  -> ContextRef ; 
@@ -422,12 +427,12 @@ native mod llvm {
422427    /* Operations on global variables, functions, and aliases (globals) */ 
423428    fn  LLVMGetGlobalParent ( Global :  ValueRef )  -> ModuleRef ; 
424429    fn  LLVMIsDeclaration ( Global :  ValueRef )  -> Bool ; 
425-     fn  LLVMGetLinkage ( Global :  ValueRef )  -> Linkage ; 
426-     fn  LLVMSetLinkage ( Global :  ValueRef ,  Link :  Linkage ) ; 
430+     fn  LLVMGetLinkage ( Global :  ValueRef )  -> unsigned ; 
431+     fn  LLVMSetLinkage ( Global :  ValueRef ,  Link :  unsigned ) ; 
427432    fn  LLVMGetSection ( Global :  ValueRef )  -> sbuf ; 
428433    fn  LLVMSetSection ( Global :  ValueRef ,  Section :  sbuf ) ; 
429-     fn  LLVMGetVisibility ( Global :  ValueRef )  -> Visibility ; 
430-     fn  LLVMSetVisibility ( Global :  ValueRef ,  Viz :  Visibility ) ; 
434+     fn  LLVMGetVisibility ( Global :  ValueRef )  -> unsigned ; 
435+     fn  LLVMSetVisibility ( Global :  ValueRef ,  Viz :  unsigned ) ; 
431436    fn  LLVMGetAlignment ( Global :  ValueRef )  -> unsigned ; 
432437    fn  LLVMSetAlignment ( Global :  ValueRef ,  Bytes :  unsigned ) ; 
433438
@@ -469,9 +474,9 @@ native mod llvm {
469474    fn  LLVMSetFunctionCallConv ( Fn :  ValueRef ,  CC :  unsigned ) ; 
470475    fn  LLVMGetGC ( Fn :  ValueRef )  -> sbuf ; 
471476    fn  LLVMSetGC ( Fn :  ValueRef ,  Name :  sbuf ) ; 
472-     fn  LLVMAddFunctionAttr ( Fn :  ValueRef ,  PA :  Attribute ,  HighPA :  unsigned ) ; 
473-     fn  LLVMGetFunctionAttr ( Fn :  ValueRef )  -> Attribute ; 
474-     fn  LLVMRemoveFunctionAttr ( Fn :  ValueRef ,  PA :  Attribute ,  HighPA :  unsigned ) ; 
477+     fn  LLVMAddFunctionAttr ( Fn :  ValueRef ,  PA :  unsigned ,  HighPA :  unsigned ) ; 
478+     fn  LLVMGetFunctionAttr ( Fn :  ValueRef )  -> unsigned ; 
479+     fn  LLVMRemoveFunctionAttr ( Fn :  ValueRef ,  PA :  unsigned ,  HighPA :  unsigned ) ; 
475480
476481    /* Operations on parameters */ 
477482    fn  LLVMCountParams ( Fn :  ValueRef )  -> unsigned ; 
@@ -482,9 +487,9 @@ native mod llvm {
482487    fn  LLVMGetLastParam ( Fn :  ValueRef )  -> ValueRef ; 
483488    fn  LLVMGetNextParam ( Arg :  ValueRef )  -> ValueRef ; 
484489    fn  LLVMGetPreviousParam ( Arg :  ValueRef )  -> ValueRef ; 
485-     fn  LLVMAddAttribute ( Arg :  ValueRef ,  PA :  Attribute ) ; 
486-     fn  LLVMRemoveAttribute ( Arg :  ValueRef ,  PA :  Attribute ) ; 
487-     fn  LLVMGetAttribute ( Arg :  ValueRef )  -> Attribute ; 
490+     fn  LLVMAddAttribute ( Arg :  ValueRef ,  PA :  unsigned ) ; 
491+     fn  LLVMRemoveAttribute ( Arg :  ValueRef ,  PA :  unsigned ) ; 
492+     fn  LLVMGetAttribute ( Arg :  ValueRef )  -> unsigned ; 
488493    fn  LLVMSetParamAlignment ( Arg :  ValueRef ,  align :  unsigned ) ; 
489494
490495    /* Operations on basic blocks */ 
@@ -520,9 +525,9 @@ native mod llvm {
520525    /* Operations on call sites */ 
521526    fn  LLVMSetInstructionCallConv ( Instr :  ValueRef ,  CC :  unsigned ) ; 
522527    fn  LLVMGetInstructionCallConv ( Instr :  ValueRef )  -> unsigned ; 
523-     fn  LLVMAddInstrAttribute ( Instr :  ValueRef ,  index :  unsigned ,  IA :  Attribute ) ; 
528+     fn  LLVMAddInstrAttribute ( Instr :  ValueRef ,  index :  unsigned ,  IA :  unsigned ) ; 
524529    fn  LLVMRemoveInstrAttribute ( Instr :  ValueRef ,  index :  unsigned , 
525-                                 IA :  Attribute ) ; 
530+                                 IA :  unsigned ) ; 
526531    fn  LLVMSetInstrParamAlignment ( Instr :  ValueRef ,  index :  unsigned , 
527532                                  align :  unsigned ) ; 
528533
@@ -836,9 +841,6 @@ native mod llvm {
836841
837842    /* Stuff that's in rustllvm/ because it's not upstream yet. */ 
838843
839-     type  ObjectFileRef ; 
840-     type  SectionIteratorRef ; 
841- 
842844    /** Opens an object file. */ 
843845    fn  LLVMCreateObjectFile ( MemBuf :  MemoryBufferRef )  -> ObjectFileRef ; 
844846    /** Closes an object file. */ 
@@ -907,6 +909,16 @@ native mod llvm {
907909    fn  LLVMLinkModules ( Dest :  ModuleRef ,  Src :  ModuleRef )  -> Bool ; 
908910} 
909911
912+ fn  SetInstructionCallConv ( Instr :  ValueRef ,  CC :  CallConv )  { 
913+     llvm:: LLVMSetInstructionCallConv ( Instr ,  CC  as  unsigned ) ; 
914+ } 
915+ fn  SetFunctionCallConv ( Fn :  ValueRef ,  CC :  CallConv )  { 
916+     llvm:: LLVMSetFunctionCallConv ( Fn ,  CC  as  unsigned ) ; 
917+ } 
918+ fn  SetLinkage ( Global :  ValueRef ,  Link :  Linkage )  { 
919+     llvm:: LLVMSetLinkage ( Global ,  Link  as  unsigned ) ; 
920+ } 
921+ 
910922/* Memory-managed object interface to type handles. */ 
911923
912924type  type_names  = @{ type_names :  std:: map:: hashmap < TypeRef ,  str > , 
0 commit comments