-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathgen_string.go
120 lines (104 loc) · 2.27 KB
/
gen_string.go
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
func emitEmptyString() {
emit("LOAD_EMPTY_SLICE")
}
func countStrlen(chars []byte) int {
var length int
var isInBackSlash bool
for _, c := range chars {
if !isInBackSlash && c == '\\' {
isInBackSlash = true
continue
} else if isInBackSlash && c == '\\' {
isInBackSlash = false
length++
} else {
isInBackSlash = false
length++
}
}
return length
}
func (ast *ExprStringLiteral) emit() {
var length int = countStrlen(ast.val)
if length == 0 {
emitEmptyString()
} else {
emit("LOAD_STRING_LITERAL .%s", ast.slabel)
emit("movq $%d, %%rbx", length)
emit("movq $%d, %%rcx", length)
}
}
func (e *IrStringConcat) emit() {
emit("# IrExprStringComparison")
var args []Expr
args = append(args, e.left)
args = append(args, e.right)
var params []*ExprVariable
var dummyVariable = &ExprVariable{
isVariadic: false,
}
params = append(params, dummyVariable) // 1st arg
params = append(params, dummyVariable) // 2nd arg
// left + right
call := &IrCall{
tok: e.token(),
symbol: getFuncSymbol(IRuntimePath, "concat"),
args: args,
callee: &DeclFunc{
params: params,
},
}
call.emit()
}
func (binop *IrExprStringComparison) emit() {
emit("# IrExprStringComparison")
var equal bool
switch binop.op {
case "!=":
equal = false
case "==":
equal = true
default:
TBI(binop.token(), "")
}
// 3rd arg
var eFlag Expr
if equal {
eFlag = eTrue
} else {
eFlag = eFalse
}
var args []Expr
args = append(args, binop.left)
args = append(args, binop.right)
args = append(args, eFlag)
var params []*ExprVariable
var dummyVariable = &ExprVariable{
isVariadic: false,
}
// func eq(a string, b string, eq bool) bool
// @TODO get params by DeclFunc dynamically
params = append(params, dummyVariable) // a
params = append(params, dummyVariable) // b
params = append(params, dummyVariable) // eq
// eq(left, right, eFlag)
call := &IrCall{
tok: binop.token(),
symbol: getFuncSymbol(IRuntimePath, "cmpStrings"),
args: args,
callee: &DeclFunc{
params: params,
},
}
call.emit()
}
func emitGoStringsEqualFromStack() {
eTrue.emit()
emit("PUSH_8")
call := &IrLowLevelCall{
symbol: getFuncSymbol(IRuntimePath, "cmpStrings"),
argsFromStack: 7,
}
call.emit()
}