Skip to content

Commit ffd0534

Browse files
committed
Remove functions/methods that work on the global context
The global context should almost never be used, but it's very easy to use accidentally. Therefore, I'd like to remove all convenience functions that use the global context. All functionality that got removed is still accessible as methods on llvm.Context, and the global context can be obtained using llvm.GlobalContext(). So no actual functionality is lost, just the very easy to misuse "convenience" functions.
1 parent 9edb640 commit ffd0534

File tree

4 files changed

+16
-68
lines changed

4 files changed

+16
-68
lines changed

bitreader.go

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,31 +24,6 @@ import (
2424
"unsafe"
2525
)
2626

27-
// ParseBitcodeFile parses the LLVM IR (bitcode) in the file with the
28-
// specified name, and returns a new LLVM module.
29-
func ParseBitcodeFile(name string) (Module, error) {
30-
var buf C.LLVMMemoryBufferRef
31-
var errmsg *C.char
32-
var cfilename *C.char = C.CString(name)
33-
defer C.free(unsafe.Pointer(cfilename))
34-
result := C.LLVMCreateMemoryBufferWithContentsOfFile(cfilename, &buf, &errmsg)
35-
if result != 0 {
36-
err := errors.New(C.GoString(errmsg))
37-
C.free(unsafe.Pointer(errmsg))
38-
return Module{}, err
39-
}
40-
defer C.LLVMDisposeMemoryBuffer(buf)
41-
42-
var m Module
43-
if C.LLVMParseBitcode2(buf, &m.C) == 0 {
44-
return m, nil
45-
}
46-
47-
err := errors.New(C.GoString(errmsg))
48-
C.free(unsafe.Pointer(errmsg))
49-
return Module{}, err
50-
}
51-
5227
// ParseBitcodeFile parses the LLVM IR (bitcode) in the file with the specified
5328
// name, and returns a new LLVM module.
5429
func (c Context) ParseBitcodeFile(name string) (Module, error) {

executionengine_test.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ func TestFactorial(t *testing.T) {
2121
InitializeNativeTarget()
2222
InitializeNativeAsmPrinter()
2323

24-
mod := NewModule("fac_module")
24+
ctx := NewContext()
25+
mod := ctx.NewModule("fac_module")
2526

26-
fac_args := []Type{Int32Type()}
27-
fac_type := FunctionType(Int32Type(), fac_args, false)
27+
fac_args := []Type{ctx.Int32Type()}
28+
fac_type := FunctionType(ctx.Int32Type(), fac_args, false)
2829
fac := AddFunction(mod, "fac", fac_type)
2930
fac.SetFunctionCallConv(CCallConv)
3031
n := fac.Param(0)
@@ -34,26 +35,26 @@ func TestFactorial(t *testing.T) {
3435
iffalse := AddBasicBlock(fac, "iffalse")
3536
end := AddBasicBlock(fac, "end")
3637

37-
builder := NewBuilder()
38+
builder := ctx.NewBuilder()
3839
defer builder.Dispose()
3940

4041
builder.SetInsertPointAtEnd(entry)
41-
If := builder.CreateICmp(IntEQ, n, ConstInt(Int32Type(), 0, false), "cmptmp")
42+
If := builder.CreateICmp(IntEQ, n, ConstInt(ctx.Int32Type(), 0, false), "cmptmp")
4243
builder.CreateCondBr(If, iftrue, iffalse)
4344

4445
builder.SetInsertPointAtEnd(iftrue)
45-
res_iftrue := ConstInt(Int32Type(), 1, false)
46+
res_iftrue := ConstInt(ctx.Int32Type(), 1, false)
4647
builder.CreateBr(end)
4748

4849
builder.SetInsertPointAtEnd(iffalse)
49-
n_minus := builder.CreateSub(n, ConstInt(Int32Type(), 1, false), "subtmp")
50+
n_minus := builder.CreateSub(n, ConstInt(ctx.Int32Type(), 1, false), "subtmp")
5051
call_fac_args := []Value{n_minus}
5152
call_fac := builder.CreateCall(fac_type, fac, call_fac_args, "calltmp")
5253
res_iffalse := builder.CreateMul(n, call_fac, "multmp")
5354
builder.CreateBr(end)
5455

5556
builder.SetInsertPointAtEnd(end)
56-
res := builder.CreatePHI(Int32Type(), "result")
57+
res := builder.CreatePHI(ctx.Int32Type(), "result")
5758
phi_vals := []Value{res_iftrue, res_iffalse}
5859
phi_blocks := []BasicBlock{iftrue, iffalse}
5960
res.AddIncoming(phi_vals, phi_blocks)
@@ -87,7 +88,7 @@ func TestFactorial(t *testing.T) {
8788
pass.AddCFGSimplificationPass()
8889
pass.Run(mod)
8990

90-
exec_args := []GenericValue{NewGenericValueFromInt(Int32Type(), 10, false)}
91+
exec_args := []GenericValue{NewGenericValueFromInt(ctx.Int32Type(), 10, false)}
9192
exec_res := engine.RunFunction(fac, exec_args)
9293
var fac10 uint64 = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1
9394
if exec_res.Int(false) != fac10 {

ir.go

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -448,13 +448,6 @@ func (a Attribute) IsString() bool {
448448

449449
// Create and destroy modules.
450450
// See llvm::Module::Module.
451-
func NewModule(name string) (m Module) {
452-
cname := C.CString(name)
453-
defer C.free(unsafe.Pointer(cname))
454-
m.C = C.LLVMModuleCreateWithName(cname)
455-
return
456-
}
457-
458451
func (c Context) NewModule(name string) (m Module) {
459452
cname := C.CString(name)
460453
defer C.free(unsafe.Pointer(cname))
@@ -563,17 +556,6 @@ func (c Context) IntType(numbits int) (t Type) {
563556
return
564557
}
565558

566-
func Int1Type() (t Type) { t.C = C.LLVMInt1Type(); return }
567-
func Int8Type() (t Type) { t.C = C.LLVMInt8Type(); return }
568-
func Int16Type() (t Type) { t.C = C.LLVMInt16Type(); return }
569-
func Int32Type() (t Type) { t.C = C.LLVMInt32Type(); return }
570-
func Int64Type() (t Type) { t.C = C.LLVMInt64Type(); return }
571-
572-
func IntType(numbits int) (t Type) {
573-
t.C = C.LLVMIntType(C.unsigned(numbits))
574-
return
575-
}
576-
577559
func (t Type) IntTypeWidth() int {
578560
return int(C.LLVMGetIntTypeWidth(t.C))
579561
}
@@ -585,12 +567,6 @@ func (c Context) X86FP80Type() (t Type) { t.C = C.LLVMX86FP80TypeInContext(c.C)
585567
func (c Context) FP128Type() (t Type) { t.C = C.LLVMFP128TypeInContext(c.C); return }
586568
func (c Context) PPCFP128Type() (t Type) { t.C = C.LLVMPPCFP128TypeInContext(c.C); return }
587569

588-
func FloatType() (t Type) { t.C = C.LLVMFloatType(); return }
589-
func DoubleType() (t Type) { t.C = C.LLVMDoubleType(); return }
590-
func X86FP80Type() (t Type) { t.C = C.LLVMX86FP80Type(); return }
591-
func FP128Type() (t Type) { t.C = C.LLVMFP128Type(); return }
592-
func PPCFP128Type() (t Type) { t.C = C.LLVMPPCFP128Type(); return }
593-
594570
// Operations on function types
595571
func FunctionType(returnType Type, paramTypes []Type, isVarArg bool) (t Type) {
596572
var pt *C.LLVMTypeRef
@@ -706,9 +682,6 @@ func (c Context) VoidType() (t Type) { t.C = C.LLVMVoidTypeInContext(c.C); retu
706682
func (c Context) LabelType() (t Type) { t.C = C.LLVMLabelTypeInContext(c.C); return }
707683
func (c Context) TokenType() (t Type) { t.C = C.LLVMTokenTypeInContext(c.C); return }
708684

709-
func VoidType() (t Type) { t.C = C.LLVMVoidType(); return }
710-
func LabelType() (t Type) { t.C = C.LLVMLabelType(); return }
711-
712685
//-------------------------------------------------------------------------
713686
// llvm.Value
714687
//-------------------------------------------------------------------------
@@ -1363,7 +1336,6 @@ func (v Value) AllocatedType() (t Type) { t.C = C.LLVMGetAllocatedType(v.C); ret
13631336
// exclusive means of building instructions using the C interface.
13641337

13651338
func (c Context) NewBuilder() (b Builder) { b.C = C.LLVMCreateBuilderInContext(c.C); return }
1366-
func NewBuilder() (b Builder) { b.C = C.LLVMCreateBuilder(); return }
13671339
func (b Builder) SetInsertPoint(block BasicBlock, instr Value) {
13681340
C.LLVMPositionBuilder(b.C, block.C, instr.C)
13691341
}
@@ -1402,7 +1374,7 @@ func (b Builder) GetCurrentDebugLocation() (loc DebugLoc) {
14021374
func (b Builder) SetInstDebugLocation(v Value) { C.LLVMSetInstDebugLocation(b.C, v.C) }
14031375
func (b Builder) InsertDeclare(module Module, storage Value, md Value) Value {
14041376
f := module.NamedFunction("llvm.dbg.declare")
1405-
ftyp := FunctionType(VoidType(), []Type{storage.Type(), md.Type()}, false)
1377+
ftyp := FunctionType(module.Context().VoidType(), []Type{storage.Type(), md.Type()}, false)
14061378
if f.IsNil() {
14071379
f = AddFunction(module, "llvm.dbg.declare", ftyp)
14081380
}

ir_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ import (
1919
)
2020

2121
func testAttribute(t *testing.T, name string) {
22-
mod := NewModule("")
22+
ctx := NewContext()
23+
mod := ctx.NewModule("")
2324
defer mod.Dispose()
2425

25-
ftyp := FunctionType(VoidType(), nil, false)
26+
ftyp := FunctionType(ctx.VoidType(), nil, false)
2627
fn := AddFunction(mod, "foo", ftyp)
2728

2829
kind := AttributeKindID(name)
@@ -104,11 +105,10 @@ func TestAttributes(t *testing.T) {
104105
}
105106

106107
func TestDebugLoc(t *testing.T) {
107-
mod := NewModule("")
108+
ctx := NewContext()
109+
mod := ctx.NewModule("")
108110
defer mod.Dispose()
109111

110-
ctx := mod.Context()
111-
112112
b := ctx.NewBuilder()
113113
defer b.Dispose()
114114

0 commit comments

Comments
 (0)