-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathllvmutil.h
98 lines (77 loc) · 2.92 KB
/
llvmutil.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
* llvmutil.h -- utility things for use with the LLVM C API (header file)
* Copyright 2010 Kalle A. Sandström <ksandstr@iki.fi>
*
* This file is part of µiX.
*
* µiX is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* µiX is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with µiX. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SEEN_LLVMUTIL_H
#define SEEN_LLVMUTIL_H
#include <llvm-c/Core.h>
#include "defs.h"
/* shorthand for LLVM types.
*
* NOTE: these are only for declaring variables. formal function parameter
* types should still be written out.
*/
#define V LLVMValueRef
#define T LLVMTypeRef
#define BB LLVMBasicBlockRef
#define A LLVMAttributeRef
/* constant macros. more useful than piles of LLVMConstInt(ctx->i32t, ...). */
#define CONST_INT(n) LLVMConstInt(ctx->i32t, (n), 1)
#define CONST_UINT(n) LLVMConstInt(ctx->i32t, (n), 0)
#define CONST_WORD(n) LLVMConstInt(ctx->wordt, (n), 0)
/* casting */
#define WORD(v) LLVMBuildZExtOrBitCast(ctx->builder, (v), ctx->wordt, "cast.w")
/* NOTE: offset is in _words_, since that's the unit the UTCB is addressed
* with.
*/
#define UTCB_ADDR_VAL(ctx, offset, name) \
({ V _off = (offset); \
LLVMBuildGEP((ctx)->builder, (ctx)->utcb, &_off, 1, (name)); })
/* helper functions from llvmutil.c */
/* set a value @val on @phi from origin LLVMGetInsertBlock(ctx->builder). */
extern void branch_set_phi(
struct llvm_ctx *ctx,
LLVMValueRef phi,
LLVMValueRef val);
extern LLVMBasicBlockRef add_sibling_block(
struct llvm_ctx *ctx,
const char *name_fmt,
...);
/* build free insns for LLVMValueRef data in ctx->malloc_ptrs, then truncate
* the list.
*/
extern void build_free_mallocs(struct llvm_ctx *ctx);
/* get the msgerr basic block. this function must be called before
* ctx->errval_phi is referenced.
*/
extern LLVMBasicBlockRef get_msgerr_bb(struct llvm_ctx *ctx);
/* context management. */
extern struct llvm_ctx *create_llvm_ctx(struct print_ctx *pr);
extern void dispose_llvm_ctx(struct llvm_ctx *ctx);
/* function start helper. the caller should close the entry block with a call
* to end_function() after the function has been built.
*/
extern LLVMBasicBlockRef begin_function(
struct llvm_ctx *ctx,
LLVMValueRef fn);
/* called in the block where pre-return cleanups should occur. closes the entry
* block with a branch to the specified basic block.
*/
extern void end_function(struct llvm_ctx *ctx, LLVMBasicBlockRef start_bb);
extern LLVMAttributeRef llvm_attr(struct llvm_ctx *ctx, const char *attr);
#endif