Comments (3)
Glad to see you are getting into compiler design and development! It's a great passion and you will hopefully have a lot of fun implementing your own compiler :)
Given the following input C source file:
typedef int (*FooFunc)(int x, int y);
typedef double (*BarFunc)(double v);
typedef struct {
FooFunc foo_func;
BarFunc bar_func;
} StructType;
int foo(int x, int y) {
return x + y;
}
double bar(double v) {
return 2.0*v;
}
int main() {
StructType baz;
baz.foo_func = foo;
baz.bar_func = bar;
double result = baz.bar_func(3.0);
return baz.foo_func(12, 30);
}
And the corresponding LLVM IR (by running the command clang -S -emit-llvm -o foo.ll foo.c
):
; ModuleID = 'foo.c'
source_filename = "foo.c"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-linux-gnu"
%struct.StructType = type { i32 (i32, i32)*, double (double)* }
; Function Attrs: noinline nounwind optnone sspstrong uwtable
define dso_local i32 @foo(i32 %0, i32 %1) #0 {
%3 = alloca i32, align 4
%4 = alloca i32, align 4
store i32 %0, i32* %3, align 4
store i32 %1, i32* %4, align 4
%5 = load i32, i32* %3, align 4
%6 = load i32, i32* %4, align 4
%7 = add nsw i32 %5, %6
ret i32 %7
}
; Function Attrs: noinline nounwind optnone sspstrong uwtable
define dso_local double @bar(double %0) #0 {
%2 = alloca double, align 8
store double %0, double* %2, align 8
%3 = load double, double* %2, align 8
%4 = fmul double 2.000000e+00, %3
ret double %4
}
; Function Attrs: noinline nounwind optnone sspstrong uwtable
define dso_local i32 @main() #0 {
%1 = alloca i32, align 4
%2 = alloca %struct.StructType, align 8
%3 = alloca double, align 8
store i32 0, i32* %1, align 4
%4 = getelementptr inbounds %struct.StructType, %struct.StructType* %2, i32 0, i32 0
store i32 (i32, i32)* @foo, i32 (i32, i32)** %4, align 8
%5 = getelementptr inbounds %struct.StructType, %struct.StructType* %2, i32 0, i32 1
store double (double)* @bar, double (double)** %5, align 8
%6 = getelementptr inbounds %struct.StructType, %struct.StructType* %2, i32 0, i32 1
%7 = load double (double)*, double (double)** %6, align 8
%8 = call double %7(double 3.000000e+00)
store double %8, double* %3, align 8
%9 = getelementptr inbounds %struct.StructType, %struct.StructType* %2, i32 0, i32 0
%10 = load i32 (i32, i32)*, i32 (i32, i32)** %9, align 8
%11 = call i32 %10(i32 12, i32 30)
ret i32 %11
}
attributes #0 = { noinline nounwind optnone sspstrong uwtable "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
!llvm.module.flags = !{!0, !1, !2, !3, !4}
!llvm.ident = !{!5}
!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 7, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 1}
!4 = !{i32 7, !"frame-pointer", i32 2}
!5 = !{!"clang version 13.0.1"}
We can see that the struct type containing the two function pointers is as follows:
%struct.StructType = type { i32 (i32, i32)*, double (double)* }
To produce a corresponding struct type using the llir/llvm
API, the type has to be the same, both structurally (have the same struct fields with the same types) and the same type name (i.e. %struct.StructType
in this case).
package main
import (
"fmt"
"github.com/llir/llvm/ir/types"
)
func main() {
i32RetType := types.I32
i32XParamType := types.I32
i32YParamType := types.I32
fooFuncType := types.NewFunc(i32RetType, i32XParamType, i32YParamType)
doubleRetType := types.Double
doubleVParamType := types.Double
barFuncType := types.NewFunc(doubleRetType, doubleVParamType)
structType := types.NewStruct(fooFuncType, barFuncType)
structType.SetName("struct.StructType")
fmt.Println(structType)
}
The above Go program will output:
%struct.StructType
In particular, take a look at the documentation of ir/types.NewFunc.
Wish you all the best and happy coding!
Cheers,
Robin
from llvm.
Oh wow thanks for the quick reply!
I see, alright thank you a lot for taking the time to explain this to me :D
from llvm.
Glad the explanation helped! :)
I'll close this issue for now, if you wonder something in the future, feel free to open a new issue or re-open this one.
Happy coding!
Cheers,
Robin
from llvm.
Related Issues (20)
- `SRet` struct location HOT 3
- Wrong function pointer type HOT 1
- indirect br only takes constant addr HOT 1
- Returning void as a value HOT 3
- update llir/llvm to support 14.0 HOT 2
- update llir/llvm to support LLVM 15.0 HOT 7
- How to have two structs referencing eachother HOT 4
- How to convert to exe? HOT 3
- `module.NewTypeDef` has unexpected side effects HOT 17
- Provide working Hello, World example HOT 1
- support llvm 16.0.0 HOT 2
- incorrect cmpxchg signature HOT 4
- How to generate ir for embedded struct? HOT 6
- Convert to string HOT 1
- Hello 10 instead of Hello world. Passing a IntPtr to printf (go - LLIR/LLVM) HOT 3
- malloc and strcpy in LLVM HOT 1
- unable to generate LLVM: structure, external , noundef and ptr HOT 1
- No way to find underlying type of pointer or array HOT 1
- How to compile LLVM IR to machine code programatically? HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from llvm.