Last active
September 19, 2017 21:43
-
-
Save robstewart57/b11353feb69dc1a6dc30 to your computer and use it in GitHub Desktop.
LLVM function pointers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int add(int i, int j) | |
{ | |
return (i + j); | |
} | |
int sub(int i, int j) | |
{ | |
return (i - j); | |
} | |
void print(int x, int y, int (*func)()) | |
{ | |
printf("value is : %d\n", (*func)(x, y)); | |
} | |
int main() | |
{ | |
int x=100, y=200; | |
print(x,y,add); | |
print(x,y,sub); | |
return 0; | |
} | |
%%%%%%%%%%%%%%%%%%%%%%%%%% | |
; ModuleID = 'function-ptr.ll' | |
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" | |
target triple = "x86_64-unknown-linux-gnu" | |
@.str = private unnamed_addr constant [15 x i8] c"value is : %d\0A\00", align 1 | |
; Function Attrs: nounwind uwtable | |
define i32 @add(i32 %i, i32 %j) #0 { | |
entry: | |
%add = add nsw i32 %i, %j | |
ret i32 %add | |
} | |
; Function Attrs: nounwind uwtable | |
define i32 @sub(i32 %i, i32 %j) #0 { | |
entry: | |
%sub = sub nsw i32 %i, %j | |
ret i32 %sub | |
} | |
; Function Attrs: nounwind uwtable | |
define void @print(i32 %x, i32 %y, i32 (i32, i32)* %func) #0 { | |
entry: | |
%call = call i32 %func(i32 %x, i32 %y) | |
%call1 = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([15 x i8]* @.str, i32 0, i32 0), i32 %call) | |
ret void | |
} | |
declare i32 @printf(i8*, ...) #1 | |
; Function Attrs: nounwind uwtable | |
define i32 @main() #0 { | |
entry: | |
call void @print(i32 100, i32 200, i32 (i32, i32)* @add) | |
call void @print(i32 100, i32 200, i32 (i32, i32)* @sub) | |
ret i32 0 | |
} | |
attributes #0 = { nounwind uwtable "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" } | |
attributes #1 = { "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" } | |
!llvm.ident = !{!0} | |
!0 = !{!"clang version 3.7.0 (trunk 230020)"} | |
%%%%%%%%%%%%%%%%%%%%%%%%%% | |
.text | |
.file "function-ptr-mem2reg.ll" | |
.globl add | |
.align 16, 0x90 | |
.type add,@function | |
add: # @add | |
.cfi_startproc | |
# BB#0: # %entry | |
pushq %rbp | |
.Ltmp0: | |
.cfi_def_cfa_offset 16 | |
.Ltmp1: | |
.cfi_offset %rbp, -16 | |
movq %rsp, %rbp | |
.Ltmp2: | |
.cfi_def_cfa_register %rbp | |
# kill: ESI<def> ESI<kill> RSI<def> | |
# kill: EDI<def> EDI<kill> RDI<def> | |
leal (%rdi,%rsi), %eax | |
popq %rbp | |
retq | |
.Ltmp3: | |
.size add, .Ltmp3-add | |
.cfi_endproc | |
.globl sub | |
.align 16, 0x90 | |
.type sub,@function | |
sub: # @sub | |
.cfi_startproc | |
# BB#0: # %entry | |
pushq %rbp | |
.Ltmp4: | |
.cfi_def_cfa_offset 16 | |
.Ltmp5: | |
.cfi_offset %rbp, -16 | |
movq %rsp, %rbp | |
.Ltmp6: | |
.cfi_def_cfa_register %rbp | |
subl %esi, %edi | |
movl %edi, %eax | |
popq %rbp | |
retq | |
.Ltmp7: | |
.size sub, .Ltmp7-sub | |
.cfi_endproc | |
.globl print | |
.align 16, 0x90 | |
.type print,@function | |
print: # @print | |
.cfi_startproc | |
# BB#0: # %entry | |
pushq %rbp | |
.Ltmp8: | |
.cfi_def_cfa_offset 16 | |
.Ltmp9: | |
.cfi_offset %rbp, -16 | |
movq %rsp, %rbp | |
.Ltmp10: | |
.cfi_def_cfa_register %rbp | |
callq *%rdx | |
movl %eax, %ecx | |
movl $.L.str, %edi | |
xorl %eax, %eax | |
movl %ecx, %esi | |
callq printf | |
popq %rbp | |
retq | |
.Ltmp11: | |
.size print, .Ltmp11-print | |
.cfi_endproc | |
.globl main | |
.align 16, 0x90 | |
.type main,@function | |
main: # @main | |
.cfi_startproc | |
# BB#0: # %entry | |
pushq %rbp | |
.Ltmp12: | |
.cfi_def_cfa_offset 16 | |
.Ltmp13: | |
.cfi_offset %rbp, -16 | |
movq %rsp, %rbp | |
.Ltmp14: | |
.cfi_def_cfa_register %rbp | |
movl $100, %edi | |
movl $200, %esi | |
movl $add, %edx | |
callq print | |
movl $100, %edi | |
movl $200, %esi | |
movl $sub, %edx | |
callq print | |
xorl %eax, %eax | |
popq %rbp | |
retq | |
.Ltmp15: | |
.size main, .Ltmp15-main | |
.cfi_endproc | |
.type .L.str,@object # @.str | |
.section .rodata.str1.1,"aMS",@progbits,1 | |
.L.str: | |
.asciz "value is : %d\n" | |
.size .L.str, 15 | |
.ident "clang version 3.7.0 (trunk 230020)" | |
.section ".note.GNU-stack","",@progbits | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment