Skip to content

Commit c6aead7

Browse files
committed
Get rid of native types in LLVM module
Code is still somewhat kludgy because we don't have 32-bit enums. Issue #1673
1 parent f6f3d51 commit c6aead7

File tree

14 files changed

+250
-268
lines changed

14 files changed

+250
-268
lines changed

src/comp/back/link.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@ import option::none;
1616
import std::sha1::sha1;
1717
import syntax::ast;
1818
import syntax::print::pprust;
19-
import lib::llvm::llvm::ModuleRef;
20-
import lib::llvm::mk_pass_manager;
21-
import lib::llvm::mk_target_data;
22-
import lib::llvm::False;
19+
import lib::llvm::{ModuleRef, mk_pass_manager, mk_target_data, True, False};
2320
import util::filesearch;
2421

2522
enum output_type {
@@ -182,7 +179,7 @@ mod write {
182179
let MPMB = llvm::LLVMPassManagerBuilderCreate();
183180
llvm::LLVMPassManagerBuilderSetOptLevel(MPMB,
184181
opts.optimize as c_uint);
185-
llvm::LLVMPassManagerBuilderSetSizeLevel(MPMB, 0);
182+
llvm::LLVMPassManagerBuilderSetSizeLevel(MPMB, False);
186183
llvm::LLVMPassManagerBuilderSetDisableUnitAtATime(MPMB, False);
187184
llvm::LLVMPassManagerBuilderSetDisableUnrollLoops(MPMB, False);
188185
llvm::LLVMPassManagerBuilderSetDisableSimplifyLibCalls(MPMB,

src/comp/back/upcall.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ import middle::trans::common::{T_fn, T_i1, T_i8, T_i32,
55
T_int, T_nil, T_dict,
66
T_opaque_vec, T_ptr,
77
T_size_t, T_void};
8-
import lib::llvm::type_names;
9-
import lib::llvm::llvm::ModuleRef;
10-
import lib::llvm::llvm::ValueRef;
11-
import lib::llvm::llvm::TypeRef;
8+
import lib::llvm::{type_names, ModuleRef, ValueRef, TypeRef};
129

1310
type upcalls =
1411
{_fail: ValueRef,

src/comp/lib/llvm.rs

+146-134
Original file line numberDiff line numberDiff line change
@@ -1,144 +1,149 @@
11
import core::{vec, str, option};
22
import str::sbuf;
33

4-
import llvm::{TypeRef, MemoryBufferRef,
5-
PassManagerRef, TargetDataRef,
6-
ObjectFileRef, SectionIteratorRef};
74
import 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 = 0u;
21-
const LLVMFastCallConv: uint = 8u;
22-
const LLVMColdCallConv: uint = 9u;
23-
const LLVMX86StdcallCallConv: uint = 64u;
24-
const LLVMX86FastcallCallConv: uint = 65u;
25-
26-
const LLVMDefaultVisibility: uint = 0u;
27-
const LLVMHiddenVisibility: uint = 1u;
28-
const LLVMProtectedVisibility: uint = 2u;
29-
30-
const LLVMExternalLinkage: uint = 0u;
31-
const LLVMAvailableExternallyLinkage: uint = 1u;
32-
const LLVMLinkOnceAnyLinkage: uint = 2u;
33-
const LLVMLinkOnceODRLinkage: uint = 3u;
34-
const LLVMWeakAnyLinkage: uint = 4u;
35-
const LLVMWeakODRLinkage: uint = 5u;
36-
const LLVMAppendingLinkage: uint = 6u;
37-
const LLVMInternalLinkage: uint = 7u;
38-
const LLVMPrivateLinkage: uint = 8u;
39-
const LLVMDLLImportLinkage: uint = 9u;
40-
const LLVMDLLExportLinkage: uint = 10u;
41-
const LLVMExternalWeakLinkage: uint = 11u;
42-
const LLVMGhostLinkage: uint = 12u;
43-
const LLVMCommonLinkage: uint = 13u;
44-
const LLVMLinkerPrivateLinkage: uint = 14u;
45-
const LLVMLinkerPrivateWeakLinkage: uint = 15u;
46-
const LLVMLinkerPrivateWeakDefAutoLinkage: uint = 16u;
47-
48-
const LLVMZExtAttribute: uint = 1u;
49-
const LLVMSExtAttribute: uint = 2u;
50-
const LLVMNoReturnAttribute: uint = 4u;
51-
const LLVMInRegAttribute: uint = 8u;
52-
const LLVMStructRetAttribute: uint = 16u;
53-
const LLVMNoUnwindAttribute: uint = 32u;
54-
const LLVMNoAliasAttribute: uint = 64u;
55-
const LLVMByValAttribute: uint = 128u;
56-
const LLVMNestAttribute: uint = 256u;
57-
const LLVMReadNoneAttribute: uint = 512u;
58-
const LLVMReadOnlyAttribute: uint = 1024u;
59-
const LLVMNoInlineAttribute: uint = 2048u;
60-
const LLVMAlwaysInlineAttribute: uint = 4096u;
61-
const LLVMOptimizeForSizeAttribute: uint = 8192u;
62-
const LLVMStackProtectAttribute: uint = 16384u;
63-
const LLVMStackProtectReqAttribute: uint = 32768u;
64-
// 31 << 16
65-
const LLVMAlignmentAttribute: uint = 2031616u;
66-
const LLVMNoCaptureAttribute: uint = 2097152u;
67-
const LLVMNoRedZoneAttribute: uint = 4194304u;
68-
const LLVMNoImplicitFloatAttribute: uint = 8388608u;
69-
const LLVMNakedAttribute: uint = 16777216u;
70-
const LLVMInlineHintAttribute: uint = 33554432u;
71-
// 7 << 26
72-
const LLVMStackAttribute: uint = 469762048u;
73-
const LLVMReturnsTwiceAttribute: uint = 536870912u;
74-
// 1 << 30
75-
const LLVMUWTableAttribute: uint = 1073741824u;
76-
const LLVMNonLazyBindAttribute: uint = 2147483648u;
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 = 32u;
84-
const LLVMIntNE: uint = 33u;
85-
const LLVMIntUGT: uint = 34u;
86-
const LLVMIntUGE: uint = 35u;
87-
const LLVMIntULT: uint = 36u;
88-
const LLVMIntULE: uint = 37u;
89-
const LLVMIntSGT: uint = 38u;
90-
const LLVMIntSGE: uint = 39u;
91-
const LLVMIntSLT: uint = 40u;
92-
const LLVMIntSLE: uint = 41u;
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 = 1u;
99-
const LLVMRealOGT: uint = 2u;
100-
const LLVMRealOGE: uint = 3u;
101-
const LLVMRealOLT: uint = 4u;
102-
const LLVMRealOLE: uint = 5u;
103-
const LLVMRealONE: uint = 6u;
104-
105-
const LLVMRealORD: uint = 7u;
106-
const LLVMRealUNO: uint = 8u;
107-
const LLVMRealUEQ: uint = 9u;
108-
const LLVMRealUGT: uint = 10u;
109-
const LLVMRealUGE: uint = 11u;
110-
const LLVMRealULT: uint = 12u;
111-
const LLVMRealULE: uint = 13u;
112-
const LLVMRealUNE: uint = 14u;
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"]
117146
native mod 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

912924
type type_names = @{type_names: std::map::hashmap<TypeRef, str>,

src/comp/middle/debuginfo.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import core::{vec, str, option, sys, ctypes, unsafe};
22
import std::fs;
33
import std::map::hashmap;
44
import lib::llvm::llvm;
5-
import lib::llvm::llvm::ValueRef;
5+
import lib::llvm::ValueRef;
66
import trans::common::*;
77
import trans::base;
88
import trans::build::B;

0 commit comments

Comments
 (0)