add clang output

Dibyendu Majumdar 9 years ago
parent d69ff33d7d
commit f5f490c2f9

@ -0,0 +1,4 @@
CLANG output
============
To help with LLVM IR code generation I am using clang's ability to dump the IR code. I construct small C programs to represent certain Lua op codes and the use clang to generate the IR. This then forms the basis for the code generation in Ravi.

@ -0,0 +1,321 @@
extern int printf(const char *, ...);
struct GCObject {
struct GCObject *next;
unsigned char tt;
unsigned char marked;
};
struct lua_State;
union Value {
struct GCObject *gc;
void *p;
int b;
int (*f)(struct lua_State *);
long long i;
double n;
};
struct TValue {
union Value value_;
int tt_;
};
struct TString {
struct GCObject *next;
unsigned char tt;
unsigned char marked;
unsigned char extra;
unsigned int hash;
unsigned long long len;
struct TString *hnext;
};
union MaxAlign {
double u;
void *s;
long long i;
long l;
};
union UTString {
union MaxAlign dummy;
struct TString tsv;
};
struct Udata {
struct GCObject *next;
unsigned char tt;
unsigned char marked;
unsigned char ttuv_; /* user value's tag */
struct Table *metatable;
unsigned long long len; /* number of bytes */
union Value user_; /* user value */
};
union UUdata {
union MaxAlign dummy; /* ensures maximum alignment for 'local' udata */
struct Udata uv;
};
struct Upvaldesc {
struct TString *name; /* upvalue name (for debug information) */
unsigned char instack; /* whether it is in stack */
unsigned char
idx; /* index of upvalue (in stack or in outer function's list) */
};
enum ravitype_t {
RAVI_TANY, /* Lua dynamic type */
RAVI_TNUMINT, /* integer number */
RAVI_TNUMFLT, /* floating point number */
RAVI_TARRAYINT, /* array of ints */
RAVI_TARRAYFLT, /* array of doubles */
RAVI_TFUNCTION,
RAVI_TTABLE,
RAVI_TSTRING,
RAVI_TNIL,
RAVI_TBOOLEAN
};
struct LocVar {
struct TString *varname;
int startpc; /* first point where variable is active */
int endpc; /* first point where variable is dead */
enum ravitype_t
ravi_type; /* RAVI type of the variable - RAVI_TANY if unknown */
};
struct Proto {
struct GCObject *next;
unsigned char tt;
unsigned char marked;
unsigned char numparams; /* number of fixed parameters */
unsigned char is_vararg;
unsigned char maxstacksize; /* maximum stack used by this function */
int sizeupvalues; /* size of 'upvalues' */
int sizek; /* size of 'k' */
int sizecode;
int sizelineinfo;
int sizep; /* size of 'p' */
int sizelocvars;
int linedefined;
int lastlinedefined;
struct TValue *k; /* constants used by the function */
unsigned int *code;
struct Proto **p; /* functions defined inside the function */
int *lineinfo; /* map from opcodes to source lines (debug information) */
struct LocVar *
locvars; /* information about local variables (debug information) */
struct Upvaldesc *upvalues; /* upvalue information */
struct LClosure *cache; /* last created closure with this prototype */
struct TString *source; /* used for debug information */
struct GCObject *gclist;
} Proto;
struct UpVal;
struct CClosure {
struct GCObject *next;
unsigned char tt;
unsigned char marked;
unsigned char nupvalues;
struct GCObject *gclist;
int (*f)(struct lua_State *);
struct TValue upvalue[1]; /* list of upvalues */
};
struct LClosure {
struct GCObject *next;
unsigned char tt;
unsigned char marked;
unsigned char nupvalues;
struct GCObject *gclist;
struct Proto *p;
struct UpVal *upvals[1]; /* list of upvalues */
};
union Closure {
struct CClosure c;
struct LClosure l;
};
union TKey {
struct {
union Value value_;
int tt_;
int next; /* for chaining (offset for next node) */
} nk;
struct TValue tvk;
};
struct Node {
struct TValue i_val;
union TKey i_key;
};
struct Table {
struct GCObject *next;
unsigned char tt;
unsigned char marked;
unsigned char flags; /* 1<<p means tagmethod(p) is not present */
unsigned char lsizenode; /* log2 of size of 'node' array */
unsigned int sizearray; /* size of 'array' array */
struct TValue *array; /* array part */
struct Node *node;
struct Node *lastfree; /* any free position is before this position */
struct Table *metatable;
struct GCObject *gclist;
enum ravitype_t ravi_array_type; /* RAVI specialization */
unsigned int ravi_array_len; /* RAVI len specialization */
};
struct Mbuffer {
char *buffer;
unsigned long long n;
unsigned long long buffsize;
};
struct stringtable {
struct TString **hash;
int nuse; /* number of elements */
int size;
};
struct lua_Debug;
typedef long long ptrdiff_t;
typedef ptrdiff_t lua_KContext;
typedef int (*lua_KFunction)(struct lua_State *L, int status, lua_KContext ctx);
typedef void *(*lua_Alloc)(void *ud, void *ptr, unsigned long long osize,
unsigned long long nsize);
typedef void (*lua_Hook)(struct lua_State *L, struct lua_Debug *ar);
struct CallInfoL { /* only for Lua functions */
struct TValue *base; /* base for this function */
const unsigned int *savedpc;
ptrdiff_t dummy;
};
struct CallInfoC { /* only for C functions */
lua_KFunction k; /* continuation in case of yields */
ptrdiff_t old_errfunc;
lua_KContext ctx; /* context info. in case of yields */
};
struct CallInfo {
struct TValue *func; /* function index in the stack */
struct TValue *top; /* top for this function */
struct CallInfo *previous, *next; /* dynamic call link */
union {
struct CallInfoL l;
struct CallInfoC c;
} u;
ptrdiff_t extra;
short nresults; /* expected number of results from this function */
unsigned char callstatus;
};
struct CallInfoLua {
struct TValue *func; /* function index in the stack */
struct TValue *top; /* top for this function */
struct CallInfo *previous, *next; /* dynamic call link */
struct CallInfoL l;
ptrdiff_t extra;
short nresults; /* expected number of results from this function */
unsigned char callstatus;
};
struct global_State;
struct lua_longjmp;
/*
** 'per thread' state
*/
struct lua_State {
struct GCObject *next;
unsigned char tt;
unsigned char marked;
unsigned char status;
struct TValue *top; /* first free slot in the stack */
struct global_State *l_G;
struct CallInfoLua *ci; /* call info for current function */
const unsigned int *oldpc; /* last pc traced */
struct TValue *stack_last; /* last free slot in the stack */
struct TValue *stack; /* stack base */
struct UpVal *openupval; /* list of open upvalues in this stack */
struct GCObject *gclist;
struct lua_State *twups; /* list of threads with open upvalues */
struct lua_longjmp *errorJmp; /* current error recover point */
struct CallInfo base_ci; /* CallInfo for first level (C calling Lua) */
lua_Hook hook;
ptrdiff_t errfunc; /* current error handling function (stack index) */
int stacksize;
int basehookcount;
int hookcount;
unsigned short nny; /* number of non-yieldable calls in stack */
unsigned short nCcalls; /* number of nested C calls */
unsigned char hookmask;
unsigned char allowhook;
};
void testfunc(struct GCObject *obj) { printf("value = %d\n", obj->tt); }
extern int luaD_poscall(struct lua_State *L, struct TValue *ra);
/*
The following represents a C version of the Lua function:
local function x()
return 1004,2
end
The Lua byte codes are:
function <(string):1,1> (4 instructions at 00000033C3366630)
0 params, 2 slots, 0 upvalues, 0 locals, 2 constants, 0 functions
1 [1] LOADK 0 -1 ; 1004
2 [1] LOADK 1 -2 ; 2
3 [1] RETURN 0 3
4 [1] RETURN 0 1
constants (2) for 00000033C3366630:
1 1004
2 2
locals (0) for 00000033C3366630:
upvalues (0) for 00000033C3366630:
*/
void test1(struct lua_State *L) {
/* This is the function prologue */
struct CallInfoLua *ci;
struct LClosure *cl;
struct TValue *k;
struct TValue *base;
struct CallInfoL *cil;
static_assert(sizeof(struct CallInfoL) == sizeof(struct CallInfoC),
"callInfoL != callInfoC");
ci = L->ci;
base = ci->l.base;
cl = (struct LClosure *)(ci->func->value_.gc);
k = cl->p->k;
/* First LOADK instruction */
struct TValue *ra = base + 0;
struct TValue *rb = k + 0;
*ra = *rb;
/* Second LOADK instruction */
struct TValue *ra2 = base + 1;
struct TValue *rb2 = k + 1;
*ra2 = *rb2;
/* OP_RETURN instruction */
int b = 3;
struct TValue *ra3 = base + 0;
// if (b)
L->top = ra3 + b - 1;
b = luaD_poscall(L, ra3);
if (b)
L->top = ci->top;
}

@ -0,0 +1,121 @@
; ModuleID = 'lua_op_loadk_return.c'
target datalayout = "e-m:w-p:32:32-i64:64-f80:32-n8:16:32-S32"
target triple = "i686-pc-windows-gnu"
%struct.Proto = type { %struct.GCObject*, i8, i8, i8, i8, i8, i32, i32, i32, i32, i32, i32, i32, i32, %struct.TValue*, i32*, %struct.Proto**, i32*, %struct.LocVar*, %struct.Upvaldesc*, %struct.LClosure*, %struct.TString*, %struct.GCObject* }
%struct.TValue = type { %union.Value, i32 }
%union.Value = type { i64 }
%struct.LocVar = type { %struct.TString*, i32, i32, i32 }
%struct.Upvaldesc = type { %struct.TString*, i8, i8 }
%struct.LClosure = type { %struct.GCObject*, i8, i8, i8, %struct.GCObject*, %struct.Proto*, [1 x %struct.UpVal*] }
%struct.UpVal = type opaque
%struct.TString = type { %struct.GCObject*, i8, i8, i8, i32, i64, %struct.TString* }
%struct.GCObject = type { %struct.GCObject*, i8, i8 }
%struct.lua_State = type { %struct.GCObject*, i8, i8, i8, %struct.TValue*, %struct.global_State*, %struct.CallInfoLua*, i32*, %struct.TValue*, %struct.TValue*, %struct.UpVal*, %struct.GCObject*, %struct.lua_State*, %struct.lua_longjmp*, %struct.CallInfo, void (%struct.lua_State*, %struct.lua_Debug*)*, i64, i32, i32, i32, i16, i16, i8, i8 }
%struct.global_State = type opaque
%struct.CallInfoLua = type { %struct.TValue*, %struct.TValue*, %struct.CallInfo*, %struct.CallInfo*, %struct.CallInfoL, i64, i16, i8 }
%struct.CallInfoL = type { %struct.TValue*, i32*, i64 }
%struct.lua_longjmp = type opaque
%struct.CallInfo = type { %struct.TValue*, %struct.TValue*, %struct.CallInfo*, %struct.CallInfo*, %union.anon, i64, i16, i8 }
%union.anon = type { %struct.CallInfoC }
%struct.CallInfoC = type { i32 (%struct.lua_State*, i32, i64)*, i64, i64 }
%struct.lua_Debug = type opaque
@.str = private unnamed_addr constant [12 x i8] c"value = %d\0A\00", align 1
@.str1 = private unnamed_addr constant [23 x i8] c"callInfoL != callInfoC\00", align 1
@Proto = common global %struct.Proto zeroinitializer, align 4
; Function Attrs: nounwind
define void @testfunc(%struct.GCObject* nocapture readonly %obj) #0 {
entry:
%tt = getelementptr inbounds %struct.GCObject* %obj, i32 0, i32 1
%0 = load i8* %tt, align 1, !tbaa !1
%conv = zext i8 %0 to i32
%call = tail call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([12 x i8]* @.str, i32 0, i32 0), i32 %conv) #2
ret void
}
; Function Attrs: nounwind
declare i32 @printf(i8* nocapture readonly, ...) #0
; Function Attrs: nounwind
define void @test1(%struct.lua_State* %L) #0 {
entry:
%call = tail call i32 bitcast (i32 (...)* @static_assert to i32 (i32, i8*)*)(i32 0, i8* getelementptr inbounds ([23 x i8]* @.str1, i32 0, i32 0)) #2
%ci1 = getelementptr inbounds %struct.lua_State* %L, i32 0, i32 6
%0 = load %struct.CallInfoLua** %ci1, align 4, !tbaa !6
%base2 = getelementptr inbounds %struct.CallInfoLua* %0, i32 0, i32 4, i32 0
%1 = load %struct.TValue** %base2, align 4, !tbaa !12
%2 = bitcast %struct.CallInfoLua* %0 to %struct.LClosure***
%3 = load %struct.LClosure*** %2, align 4, !tbaa !15
%4 = load %struct.LClosure** %3, align 4, !tbaa !16
%p = getelementptr inbounds %struct.LClosure* %4, i32 0, i32 5
%5 = load %struct.Proto** %p, align 4, !tbaa !17
%k3 = getelementptr inbounds %struct.Proto* %5, i32 0, i32 14
%6 = load %struct.TValue** %k3, align 4, !tbaa !19
%7 = bitcast %struct.TValue* %1 to i8*
%8 = bitcast %struct.TValue* %6 to i8*
tail call void @llvm.memcpy.p0i8.p0i8.i32(i8* %7, i8* %8, i32 16, i32 8, i1 false), !tbaa.struct !21
%add.ptr5 = getelementptr inbounds %struct.TValue* %1, i32 1
%add.ptr6 = getelementptr inbounds %struct.TValue* %6, i32 1
%9 = bitcast %struct.TValue* %add.ptr5 to i8*
%10 = bitcast %struct.TValue* %add.ptr6 to i8*
tail call void @llvm.memcpy.p0i8.p0i8.i32(i8* %9, i8* %10, i32 16, i32 8, i1 false), !tbaa.struct !21
%add.ptr9 = getelementptr inbounds %struct.TValue* %1, i32 2
%top = getelementptr inbounds %struct.lua_State* %L, i32 0, i32 4
store %struct.TValue* %add.ptr9, %struct.TValue** %top, align 4, !tbaa !26
%call10 = tail call i32 @luaD_poscall(%struct.lua_State* %L, %struct.TValue* %1) #2
%tobool = icmp eq i32 %call10, 0
br i1 %tobool, label %if.end, label %if.then
if.then: ; preds = %entry
%top11 = getelementptr inbounds %struct.CallInfoLua* %0, i32 0, i32 1
%11 = load %struct.TValue** %top11, align 4, !tbaa !27
store %struct.TValue* %11, %struct.TValue** %top, align 4, !tbaa !26
br label %if.end
if.end: ; preds = %entry, %if.then
ret void
}
declare i32 @static_assert(...) #1
; Function Attrs: nounwind
declare void @llvm.memcpy.p0i8.p0i8.i32(i8* nocapture, i8* nocapture readonly, i32, i32, i1) #2
declare i32 @luaD_poscall(%struct.lua_State*, %struct.TValue*) #1
attributes #0 = { nounwind "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" }
attributes #2 = { nounwind }
!llvm.ident = !{!0}
!0 = metadata !{metadata !"clang version 3.6.0 (trunk)"}
!1 = metadata !{metadata !2, metadata !4, i64 4}
!2 = metadata !{metadata !"GCObject", metadata !3, i64 0, metadata !4, i64 4, metadata !4, i64 5}
!3 = metadata !{metadata !"any pointer", metadata !4, i64 0}
!4 = metadata !{metadata !"omnipotent char", metadata !5, i64 0}
!5 = metadata !{metadata !"Simple C/C++ TBAA"}
!6 = metadata !{metadata !7, metadata !3, i64 16}
!7 = metadata !{metadata !"lua_State", metadata !3, i64 0, metadata !4, i64 4, metadata !4, i64 5, metadata !4, i64 6, metadata !3, i64 8, metadata !3, i64 12, metadata !3, i64 16, metadata !3, i64 20, metadata !3, i64 24, metadata !3, i64 28, metadata !3, i64 32, metadata !3, i64 36, metadata !3, i64 40, metadata !3, i64 44, metadata !8, i64 48, metadata !3, i64 104, metadata !9, i64 112, metadata !11, i64 120, metadata !11, i64 124, metadata !11, i64 128, metadata !10, i64 132, metadata !10, i64 134, metadata !4, i64 136, metadata !4, i64 137}
!8 = metadata !{metadata !"CallInfo", metadata !3, i64 0, metadata !3, i64 4, metadata !3, i64 8, metadata !3, i64 12, metadata !4, i64 16, metadata !9, i64 40, metadata !10, i64 48, metadata !4, i64 50}
!9 = metadata !{metadata !"long long", metadata !4, i64 0}
!10 = metadata !{metadata !"short", metadata !4, i64 0}
!11 = metadata !{metadata !"int", metadata !4, i64 0}
!12 = metadata !{metadata !13, metadata !3, i64 16}
!13 = metadata !{metadata !"CallInfoLua", metadata !3, i64 0, metadata !3, i64 4, metadata !3, i64 8, metadata !3, i64 12, metadata !14, i64 16, metadata !9, i64 32, metadata !10, i64 40, metadata !4, i64 42}
!14 = metadata !{metadata !"CallInfoL", metadata !3, i64 0, metadata !3, i64 4, metadata !9, i64 8}
!15 = metadata !{metadata !13, metadata !3, i64 0}
!16 = metadata !{metadata !3, metadata !3, i64 0}
!17 = metadata !{metadata !18, metadata !3, i64 12}
!18 = metadata !{metadata !"LClosure", metadata !3, i64 0, metadata !4, i64 4, metadata !4, i64 5, metadata !4, i64 6, metadata !3, i64 8, metadata !3, i64 12, metadata !4, i64 16}
!19 = metadata !{metadata !20, metadata !3, i64 44}
!20 = metadata !{metadata !"Proto", metadata !3, i64 0, metadata !4, i64 4, metadata !4, i64 5, metadata !4, i64 6, metadata !4, i64 7, metadata !4, i64 8, metadata !11, i64 12, metadata !11, i64 16, metadata !11, i64 20, metadata !11, i64 24, metadata !11, i64 28, metadata !11, i64 32, metadata !11, i64 36, metadata !11, i64 40, metadata !3, i64 44, metadata !3, i64 48, metadata !3, i64 52, metadata !3, i64 56, metadata !3, i64 60, metadata !3, i64 64, metadata !3, i64 68, metadata !3, i64 72, metadata !3, i64 76}
!21 = metadata !{i64 0, i64 4, metadata !16, i64 0, i64 4, metadata !16, i64 0, i64 4, metadata !22, i64 0, i64 4, metadata !16, i64 0, i64 8, metadata !23, i64 0, i64 8, metadata !24, i64 8, i64 4, metadata !22}
!22 = metadata !{metadata !11, metadata !11, i64 0}
!23 = metadata !{metadata !9, metadata !9, i64 0}
!24 = metadata !{metadata !25, metadata !25, i64 0}
!25 = metadata !{metadata !"double", metadata !4, i64 0}
!26 = metadata !{metadata !7, metadata !3, i64 8}
!27 = metadata !{metadata !13, metadata !3, i64 4}

@ -0,0 +1,4 @@
setlocal
set PATH=%PATH%;"c:\Program Files (x86)\LLVM\bin"
clang -S -emit-llvm -O %1
Loading…
Cancel
Save