annotate gen/irstate.c @ 54:28e99b04a132 trunk

[svn r58] Fixed cond expression resulting in a non-basic type. Fixed identity expression for dynamic arrays. Revamped the system to keep track of lvalues and rvalues and their relations. Typedef declaration now generate the custom typeinfo. Other bugfixes.
author lindquist
date Wed, 24 Oct 2007 01:37:34 +0200
parents 8b0e809563df
children b706170e24a9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
1 /* DMDFE backend stubs
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
2 * This file contains the implementations of the backend routines.
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
3 * For dmdfe these do nothing but print a message saying the module
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
4 * has been parsed. Substitute your own behaviors for these routimes.
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
5 */
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
6
40
8b0e809563df [svn r44] Lots of bug fixes.
lindquist
parents: 31
diff changeset
7 #include <cstdarg>
8b0e809563df [svn r44] Lots of bug fixes.
lindquist
parents: 31
diff changeset
8
8b0e809563df [svn r44] Lots of bug fixes.
lindquist
parents: 31
diff changeset
9 #include "gen/llvm.h"
8b0e809563df [svn r44] Lots of bug fixes.
lindquist
parents: 31
diff changeset
10
1
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
11 #include "mtype.h"
40
8b0e809563df [svn r44] Lots of bug fixes.
lindquist
parents: 31
diff changeset
12 #include "declaration.h"
8b0e809563df [svn r44] Lots of bug fixes.
lindquist
parents: 31
diff changeset
13
4
e116aa1488e6 [svn r8] changed backend includes to always use the gen/<foo>.h prefix
lindquist
parents: 1
diff changeset
14 #include "gen/irstate.h"
40
8b0e809563df [svn r44] Lots of bug fixes.
lindquist
parents: 31
diff changeset
15 #include "tollvm.h"
1
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
16
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
17 IRState* gIR = 0;
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
18 llvm::TargetData* gTargetData = 0;
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
19
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
20 //////////////////////////////////////////////////////////////////////////////////////////
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
21 IRScope::IRScope()
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
22 {
40
8b0e809563df [svn r44] Lots of bug fixes.
lindquist
parents: 31
diff changeset
23 begin = end = NULL;
1
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
24 }
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
25
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
26 IRScope::IRScope(llvm::BasicBlock* b, llvm::BasicBlock* e)
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
27 {
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
28 begin = b;
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
29 end = e;
40
8b0e809563df [svn r44] Lots of bug fixes.
lindquist
parents: 31
diff changeset
30 builder.SetInsertPoint(b);
1
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
31 }
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
32
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
33 //////////////////////////////////////////////////////////////////////////////////////////
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
34 IRState::IRState()
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
35 {
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
36 dmodule = 0;
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
37 module = 0;
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
38 emitMain = false;
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
39 mainFunc = 0;
40
8b0e809563df [svn r44] Lots of bug fixes.
lindquist
parents: 31
diff changeset
40 ir.state = this;
8b0e809563df [svn r44] Lots of bug fixes.
lindquist
parents: 31
diff changeset
41 }
8b0e809563df [svn r44] Lots of bug fixes.
lindquist
parents: 31
diff changeset
42
8b0e809563df [svn r44] Lots of bug fixes.
lindquist
parents: 31
diff changeset
43 IRFunction& IRState::func()
8b0e809563df [svn r44] Lots of bug fixes.
lindquist
parents: 31
diff changeset
44 {
8b0e809563df [svn r44] Lots of bug fixes.
lindquist
parents: 31
diff changeset
45 assert(!functions.empty() && "Function stack is empty!");
8b0e809563df [svn r44] Lots of bug fixes.
lindquist
parents: 31
diff changeset
46 return functions.back();
1
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
47 }
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
48
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
49 llvm::Function* IRState::topfunc()
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
50 {
40
8b0e809563df [svn r44] Lots of bug fixes.
lindquist
parents: 31
diff changeset
51 assert(!functions.empty() && "Function stack is empty!");
8b0e809563df [svn r44] Lots of bug fixes.
lindquist
parents: 31
diff changeset
52 return functions.back().func;
1
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
53 }
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
54
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
55 TypeFunction* IRState::topfunctype()
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
56 {
40
8b0e809563df [svn r44] Lots of bug fixes.
lindquist
parents: 31
diff changeset
57 assert(!functions.empty() && "Function stack is empty!");
8b0e809563df [svn r44] Lots of bug fixes.
lindquist
parents: 31
diff changeset
58 return functions.back().type;
1
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
59 }
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
60
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
61 llvm::Instruction* IRState::topallocapoint()
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
62 {
40
8b0e809563df [svn r44] Lots of bug fixes.
lindquist
parents: 31
diff changeset
63 assert(!functions.empty() && "AllocaPoint stack is empty!");
8b0e809563df [svn r44] Lots of bug fixes.
lindquist
parents: 31
diff changeset
64 return functions.back().allocapoint;
1
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
65 }
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
66
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
67 IRStruct& IRState::topstruct()
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
68 {
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
69 assert(!structs.empty() && "Struct vector is empty!");
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
70 return structs.back();
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
71 }
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
72
54
28e99b04a132 [svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents: 40
diff changeset
73 IRExp* IRState::topexp()
1
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
74 {
54
28e99b04a132 [svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents: 40
diff changeset
75 return exps.empty() ? NULL : &exps.back();
1
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
76 }
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
77
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
78 IRScope& IRState::scope()
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
79 {
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
80 assert(!scopes.empty());
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
81 return scopes.back();
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
82 }
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
83
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
84 llvm::BasicBlock* IRState::scopebb()
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
85 {
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
86 return scopebegin();
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
87 }
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
88 llvm::BasicBlock* IRState::scopebegin()
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
89 {
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
90 IRScope& s = scope();
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
91 assert(s.begin);
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
92 return s.begin;
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
93 }
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
94 llvm::BasicBlock* IRState::scopeend()
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
95 {
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
96 IRScope& s = scope();
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
97 assert(s.end);
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
98 return s.end;
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
99 }
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
100 bool IRState::scopereturned()
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
101 {
14
0e86428ee567 [svn r18] * Initial support for switch statements - No string switches yet.
lindquist
parents: 6
diff changeset
102 //return scope().returned;
0e86428ee567 [svn r18] * Initial support for switch statements - No string switches yet.
lindquist
parents: 6
diff changeset
103 return !scopebb()->empty() && scopebb()->back().isTerminator();
1
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
104 }
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
105
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
106 //////////////////////////////////////////////////////////////////////////////////////////
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
107
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
108 IRStruct::IRStruct()
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
109 : recty(llvm::OpaqueType::get())
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
110 {
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
111 type = 0;
31
2841234d2aea [svn r35] * Attributes on struct fields/methods now work
lindquist
parents: 14
diff changeset
112 queueFuncs = true;
1
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
113 }
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
114
6
35d93ce68cf4 [svn r10] Updated for LLVM rev. 20070913
lindquist
parents: 4
diff changeset
115 IRStruct::IRStruct(Type* t)
1
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
116 : recty(llvm::OpaqueType::get())
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
117 {
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
118 type = t;
31
2841234d2aea [svn r35] * Attributes on struct fields/methods now work
lindquist
parents: 14
diff changeset
119 queueFuncs = true;
1
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
120 }
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
121
40
8b0e809563df [svn r44] Lots of bug fixes.
lindquist
parents: 31
diff changeset
122 //////////////////////////////////////////////////////////////////////////////////////////
8b0e809563df [svn r44] Lots of bug fixes.
lindquist
parents: 31
diff changeset
123
8b0e809563df [svn r44] Lots of bug fixes.
lindquist
parents: 31
diff changeset
124 IRFinally::IRFinally()
8b0e809563df [svn r44] Lots of bug fixes.
lindquist
parents: 31
diff changeset
125 : bb(NULL), ret(false), retval(NULL)
8b0e809563df [svn r44] Lots of bug fixes.
lindquist
parents: 31
diff changeset
126 {
8b0e809563df [svn r44] Lots of bug fixes.
lindquist
parents: 31
diff changeset
127 }
8b0e809563df [svn r44] Lots of bug fixes.
lindquist
parents: 31
diff changeset
128
8b0e809563df [svn r44] Lots of bug fixes.
lindquist
parents: 31
diff changeset
129 IRFinally::IRFinally(llvm::BasicBlock* b)
8b0e809563df [svn r44] Lots of bug fixes.
lindquist
parents: 31
diff changeset
130 : bb(b), ret(false), retval(NULL)
1
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
131 {
c53b6e3fe49a [svn r5] Initial commit. Most things are very rough.
lindquist
parents:
diff changeset
132 }
40
8b0e809563df [svn r44] Lots of bug fixes.
lindquist
parents: 31
diff changeset
133
8b0e809563df [svn r44] Lots of bug fixes.
lindquist
parents: 31
diff changeset
134 //////////////////////////////////////////////////////////////////////////////////////////
8b0e809563df [svn r44] Lots of bug fixes.
lindquist
parents: 31
diff changeset
135
8b0e809563df [svn r44] Lots of bug fixes.
lindquist
parents: 31
diff changeset
136 LLVMBuilder* IRBuilderHelper::operator->()
8b0e809563df [svn r44] Lots of bug fixes.
lindquist
parents: 31
diff changeset
137 {
8b0e809563df [svn r44] Lots of bug fixes.
lindquist
parents: 31
diff changeset
138 LLVMBuilder& b = state->scope().builder;
8b0e809563df [svn r44] Lots of bug fixes.
lindquist
parents: 31
diff changeset
139 assert(b.GetInsertBlock() != NULL);
8b0e809563df [svn r44] Lots of bug fixes.
lindquist
parents: 31
diff changeset
140 return &b;
8b0e809563df [svn r44] Lots of bug fixes.
lindquist
parents: 31
diff changeset
141 }
8b0e809563df [svn r44] Lots of bug fixes.
lindquist
parents: 31
diff changeset
142
8b0e809563df [svn r44] Lots of bug fixes.
lindquist
parents: 31
diff changeset
143 //////////////////////////////////////////////////////////////////////////////////////////
8b0e809563df [svn r44] Lots of bug fixes.
lindquist
parents: 31
diff changeset
144
8b0e809563df [svn r44] Lots of bug fixes.
lindquist
parents: 31
diff changeset
145 IRFunction::IRFunction(FuncDeclaration* fd)
8b0e809563df [svn r44] Lots of bug fixes.
lindquist
parents: 31
diff changeset
146 {
8b0e809563df [svn r44] Lots of bug fixes.
lindquist
parents: 31
diff changeset
147 decl = fd;
8b0e809563df [svn r44] Lots of bug fixes.
lindquist
parents: 31
diff changeset
148 Type* t = LLVM_DtoDType(fd->type);
8b0e809563df [svn r44] Lots of bug fixes.
lindquist
parents: 31
diff changeset
149 assert(t->ty == Tfunction);
8b0e809563df [svn r44] Lots of bug fixes.
lindquist
parents: 31
diff changeset
150 type = (TypeFunction*)t;
8b0e809563df [svn r44] Lots of bug fixes.
lindquist
parents: 31
diff changeset
151 func = NULL;
8b0e809563df [svn r44] Lots of bug fixes.
lindquist
parents: 31
diff changeset
152 allocapoint = NULL;
8b0e809563df [svn r44] Lots of bug fixes.
lindquist
parents: 31
diff changeset
153 }
54
28e99b04a132 [svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents: 40
diff changeset
154
28e99b04a132 [svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents: 40
diff changeset
155 //////////////////////////////////////////////////////////////////////////////////////////
28e99b04a132 [svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents: 40
diff changeset
156
28e99b04a132 [svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents: 40
diff changeset
157 IRExp::IRExp()
28e99b04a132 [svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents: 40
diff changeset
158 {
28e99b04a132 [svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents: 40
diff changeset
159 e1 = e2 = NULL;
28e99b04a132 [svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents: 40
diff changeset
160 v = NULL;
28e99b04a132 [svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents: 40
diff changeset
161 }
28e99b04a132 [svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents: 40
diff changeset
162
28e99b04a132 [svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents: 40
diff changeset
163 IRExp::IRExp(Expression* l, Expression* r, llvm::Value* val)
28e99b04a132 [svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents: 40
diff changeset
164 {
28e99b04a132 [svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents: 40
diff changeset
165 e1 = l;
28e99b04a132 [svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents: 40
diff changeset
166 e2 = r;
28e99b04a132 [svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents: 40
diff changeset
167 v = val;
28e99b04a132 [svn r58] Fixed cond expression resulting in a non-basic type.
lindquist
parents: 40
diff changeset
168 }