Skip to content

Commit ca5d21a

Browse files
committed
Updated from internal Google repo.
1 parent 296f8fd commit ca5d21a

37 files changed

+2658
-163
lines changed

Makefile

-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,6 @@ clean:
143143
for file in tests/*.rn crypto_class/*.rn errortests/*.rn; do exeFile=$$(echo "$$file" | sed 's/.rn$$//'); rm -f "$$exeFile"; done
144144
cd runtime ; make clean
145145
cd bootstrap/database ; make clean
146-
cd bootstrap/cbackend ; make clean
147146

148147

149148
obj: include/dedatabase.h llvm/lldatabase.h

bootstrap/Makefile

+7
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ cbackend/int2cstring.rn\
5353
cbackend/package.rn\
5454
cbackend/stringstream.rn\
5555
cbackend/stringwriter.rn\
56+
types/builtins.rn\
57+
types/package.rn\
58+
types/typebuilder.rn\
59+
types/typechecker.rn\
60+
types/typeclasses.rn\
61+
types/typeerror.rn\
62+
types/typeunifier.rn\
5663
rune.rn
5764

5865
rune: $(SRC)

bootstrap/cbackend/cbuilder.rn

+102-2
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,47 @@
1313
// limitations under the License.
1414

1515
import database as db
16+
import types
1617

1718
use clanguageclasses
19+
use ctypegen
1820
use escapestring
21+
use sym
22+
23+
relation HashedClass CurrentScope CIdentifier cascade
24+
25+
class CurrentScope(self) {
26+
27+
func open(self) {
28+
// todo
29+
}
30+
31+
func close(self) {
32+
// todo
33+
}
34+
35+
func getIdentifier(self, name: Sym, type: CTypeExpr?, bindingInstance: bool) -> CIdentifier {
36+
// todo: handle the case where the definition was in an outer scope.
37+
id = CIdentifier(name, type, false, bindingInstance)
38+
exists = self.findCIdentifier(id)
39+
if isnull(exists) {
40+
id.firstInstance = true
41+
self.insertCIdentifier(id)
42+
}
43+
return id
44+
}
45+
}
1946

2047
relation DoublyLinked CBuilder CBlock cascade
2148

22-
class CBuilder(self) {
49+
class CBuilder(self, tc: types.TypeChecker) {
2350
self.currentParamList = null(CParamList)
2451
self.currentExprList = null(CExprList)
2552
self.program = CProgram()
2653
self.currentBlock = null(CBlock)
54+
self.identifierScope = CurrentScope()
55+
self.typechecker = tc
56+
self.typeGenerator = CTypeGenerator(tc)
2757

2858
func build(self, module: db.Function) -> CProgram {
2959
module.genC(self)
@@ -35,9 +65,11 @@ class CBuilder(self) {
3565
}
3666

3767
func openScope(self) {
68+
self.identifierScope.open()
3869
}
3970

4071
func closeScope(self) {
72+
self.identifierScope.close()
4173
}
4274

4375
func openBlock(self, location: db.Location?) {
@@ -85,10 +117,78 @@ class CBuilder(self) {
85117
return CParameter(location, name, type)
86118
}
87119

120+
func cIdentifier(self, symbol: Sym, bindingInstance: bool) -> CExpr {
121+
type = self.typechecker.symbol(symbol)
122+
if isnull(type) {
123+
ctype = null(CTypeExpr)
124+
} else {
125+
ctype = self.typeGenerator.genCType(type!)
126+
}
127+
id = self.identifierScope.getIdentifier(symbol, ctype, bindingInstance)
128+
if id.firstInstance {
129+
if !isnull(self.currentBlock) {
130+
self.currentBlock.declare(id)
131+
} else {
132+
self.program.mainCBlock.declare(id)
133+
}
134+
}
135+
return CExpr(id)
136+
}
137+
88138
func cType(self, type: CType) -> CTypeExpr {
89139
return CTypeExpr(type)
90140
}
91141

142+
func cIntType(self, location: db.Location, isSigned: bool, width: u32) -> CTypeExpr {
143+
if isSigned {
144+
if width <= 8 {
145+
prim = CPrimitiveType(CPrimitiveType.Type.Int8)
146+
} else if width <= 16 {
147+
prim = CPrimitiveType(CPrimitiveType.Type.Int16)
148+
} else if width <= 32 {
149+
prim = CPrimitiveType(CPrimitiveType.Type.Int32)
150+
} else if width <= 64 {
151+
prim = CPrimitiveType(CPrimitiveType.Type.Int64)
152+
} else {
153+
raise Status.Unimplemented, "No support for primitive integers of width larger than 64"
154+
}
155+
} else {
156+
if width <= 8 {
157+
prim = CPrimitiveType(CPrimitiveType.Type.Uint8)
158+
} else if width <= 16 {
159+
prim = CPrimitiveType(CPrimitiveType.Type.Uint16)
160+
} else if width <= 32 {
161+
prim = CPrimitiveType(CPrimitiveType.Type.Uint32)
162+
} else if width <= 64 {
163+
prim = CPrimitiveType(CPrimitiveType.Type.Uint64)
164+
} else {
165+
raise Status.Unimplemented, "No support for primitive integers of width larger than 64"
166+
}
167+
}
168+
return CTypeExpr(prim)
169+
}
170+
171+
func cFloatType(self, location: db.Location, width: u32) -> CTypeExpr {
172+
if width == 32 {
173+
return CTypeExpr(CPrimitiveType(CPrimitiveType.Type.Float))
174+
} else if width == 64 {
175+
return CTypeExpr(CPrimitiveType(CPrimitiveType.Type.Double))
176+
}
177+
raise Status.Unimplemented, "No support for floating point width %u32." % width
178+
}
179+
180+
func cStringType(self, location: db.Location) -> CTypeExpr {
181+
return CTypeExpr(CDefinedType("string"))
182+
}
183+
184+
func cBooleanType(self, location: db.Location) -> CTypeExpr {
185+
return CTypeExpr(CPrimitiveType(CPrimitiveType.Type.Int8))
186+
}
187+
188+
func cVoidType(self, location: db.Location) -> CTypeExpr {
189+
return CTypeExpr(CPrimitiveType(CPrimitiveType.Type.Void))
190+
}
191+
92192
/* Rune expressions *********************************************************************/
93193

94194
func openExprList(self) {
@@ -109,7 +209,7 @@ class CBuilder(self) {
109209
}
110210

111211
func cStringLiteral(self, value: string) -> CExpr {
112-
return CExpr(CLiteral(escapeString(value)))
212+
return CExpr(CLiteral(value))
113213
}
114214

115215
func cBoolLiteral(self, value: bool) -> CExpr {

bootstrap/cbackend/cemitter.rn

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414

1515
import database as db
1616
import io
17-
use stringwriter
17+
import types
18+
use clanguageclasses
1819
use cruntime
20+
use stringwriter
1921

2022
class CEmitter(self, filename: string) {
2123
// TODO: add buffers to string_writer so that it flushes to file every 4K or so.

0 commit comments

Comments
 (0)