This repository has been archived by the owner on Jul 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
/
qsort.go
247 lines (227 loc) · 5.67 KB
/
qsort.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"rsc.io/c2go/cc"
)
func fixQsort(prog *cc.Prog, x *cc.Expr) {
if len(x.List) != 4 {
fprintf(x.Span, "unsupported %v - wrong arg count", x)
return
}
if x.List[2].Op != cc.SizeofExpr || unparen(x.List[2].Left).String() != unparen(x.List[0]).String()+"[0]" {
fprintf(x.Span, "unsupported %v - wrong elem size %v vs %v", x, unparen(x.List[2].Left).String(), unparen(x.List[0]).String()+"[0]")
return
}
if x.List[3].Op != cc.Name || x.List[3].XDecl == nil {
fprintf(x.Span, "unsupported %v - unknown comparison function", x)
return
}
cmp := x.List[3].XDecl.Name
decl := x.List[3].XDecl
typ, newDecl := fixQsortCmp(decl)
if typ == nil {
return
}
x.Left.Text = "sort.Sort"
x.Left.XDecl = nil
x.List = []*cc.Expr{
&cc.Expr{
Op: cc.Call,
Left: &cc.Expr{Op: cc.Name, Text: cmp},
List: []*cc.Expr{
{
Op: ExprSlice,
List: []*cc.Expr{
x.List[0],
nil,
x.List[1],
},
},
},
},
}
typeDecl := &cc.Decl{
GoPackage: decl.GoPackage,
SyntaxInfo: cc.SyntaxInfo{Span: decl.Span},
Name: cmp,
Storage: cc.Typedef,
Type: &cc.Type{
Kind: Slice,
Base: typ,
},
}
lenFunc := &cc.Decl{
GoPackage: decl.GoPackage,
SyntaxInfo: cc.SyntaxInfo{Span: decl.Span},
Name: "(x " + cmp + ") Len",
Type: &cc.Type{
Kind: cc.Func,
Base: intType,
},
Body: &cc.Stmt{
Op: cc.Block,
Block: []*cc.Stmt{
{
Op: cc.Return,
Expr: &cc.Expr{
Op: cc.Name,
Text: "len(x)",
},
},
},
},
}
swapFunc := &cc.Decl{
GoPackage: decl.GoPackage,
SyntaxInfo: cc.SyntaxInfo{Span: decl.Span},
Name: "(x " + cmp + ") Swap",
Type: &cc.Type{
Kind: cc.Func,
Base: &cc.Type{Kind: cc.Void},
Decls: []*cc.Decl{
{Name: "i", Type: &cc.Type{Kind: cc.TypedefType}},
{Name: "j", Type: intType},
},
},
Body: &cc.Stmt{
Op: cc.Block,
Block: []*cc.Stmt{
{
Op: cc.StmtExpr,
Expr: &cc.Expr{
Op: cc.Name,
Text: "x[i], x[j] = x[j], x[i]",
},
},
},
},
}
for i, d := range prog.Decls {
if d == decl {
prog.Decls = append(append(prog.Decls[:i:i], typeDecl, lenFunc, swapFunc, newDecl), prog.Decls[i+1:]...)
return
}
}
prog.Decls = append(prog.Decls[:len(prog.Decls):len(prog.Decls)], typeDecl, lenFunc, swapFunc, newDecl)
}
// isGoVoidPtr reports whether t is a void* or the Go translation of a void* (*struct{}).
func isGoVoidPtr(t *cc.Type) bool {
if t == nil || t.Kind != cc.Ptr {
return false
}
t = t.Base
return t.Kind == cc.Void || t.Kind == cc.Struct && len(t.Decls) == 0
}
func fixQsortCmp(decl *cc.Decl) (*cc.Type, *cc.Decl) {
ftyp := decl.Type
if ftyp.Kind != cc.Func || len(ftyp.Decls) != 2 || !isEmptyInterface(ftyp.Decls[0].Type) || !isEmptyInterface(ftyp.Decls[1].Type) {
fprintf(decl.Span, "invalid qsort cmp function %v - wrong args", GoString(ftyp))
return nil, nil
}
a1, a2 := ftyp.Decls[0], ftyp.Decls[1]
var eq1, eq2, p1, p2 *cc.Expr
var indir1, indir2 bool
cc.Preorder(decl.Body, func(x cc.Syntax) {
switch x := x.(type) {
case *cc.Expr:
if x.Op != cc.Eq {
return
}
r := x.Right
if r.Op == cc.Indir {
r = r.Left
}
if (r.Op == TypeAssert || r.Op == cc.Cast) && r.Left.Op == cc.Name {
if r.Left.XDecl == a1 && p1 == nil {
p1 = x.Left
eq1 = x
indir1 = r != x.Right
}
if r.Left.XDecl == a2 && p2 == nil {
p2 = x.Left
eq2 = x
indir2 = r != x.Right
}
}
}
})
if p1 == nil || p2 == nil {
fprintf(decl.Span, "invalid qsort cmp function - cannot find arg extraction")
return nil, nil
}
if !sameType(p1.XType, p2.XType) {
fprintf(decl.Span, "invalid qsort cmp function - different arg types %v and %v", GoString(p1.XType), GoString(p2.XType))
return nil, nil
}
if indir1 != indir2 {
fprintf(decl.Span, "invalid qsort cmp function - different arg indirection")
return nil, nil
}
typ := p1.XType
if !indir1 {
if typ.Def().Kind != cc.Ptr {
fprintf(decl.Span, "invalid qsort cmp function - arg ptr cast to non-ptr %v", GoString(typ))
return nil, nil
}
typ = typ.Def().Base
}
// Have all the information. Committed.
// Rewrite to take x, i, j, use x[i] for p1, x[j] for p2,
// take address of x[i], x[j] if there was no indirect,
// replace all return z with return z < 0.
newDecl := *decl
decl.Body = nil
decl = &newDecl
cmp := decl.Name
decl.Name = "(x " + cmp + ") Less"
decl.Type = &cc.Type{
Kind: cc.Func,
Base: boolType,
Decls: []*cc.Decl{
{Name: "i", Type: &cc.Type{Kind: cc.TypedefType}},
{Name: "j", Type: intType},
},
}
prefix := ""
if !indir1 {
prefix = "&"
}
eq1.Right = &cc.Expr{Op: cc.Name, Text: prefix + "x[i]", XType: p1.XType}
eq2.Right = &cc.Expr{Op: cc.Name, Text: prefix + "x[j]", XType: p1.XType}
cc.Preorder(decl.Body, func(x cc.Syntax) {
switch x := x.(type) {
case *cc.Stmt:
if x.Op == cc.Return && x.Expr != nil {
ret := x.Expr
// Pick off 0, -1, +1.
// Otherwise rewrite ret to ret < 0.
switch ret.Op {
case cc.Minus, cc.Plus:
if ret.Left.Op == cc.Number && ret.Left.Text == "1" {
if ret.Op == cc.Plus {
ret.Text = "false"
} else {
ret.Text = "true"
}
ret.Op = cc.Name
ret.Left = nil
ret.XType = boolType
return
}
case cc.Number:
if ret.Text == "0" {
ret.Op = cc.Name
ret.Text = "false"
ret.XType = boolType
return
}
}
x.Expr = &cc.Expr{Op: cc.Lt, Left: ret, Right: &cc.Expr{Op: cc.Number, Text: "0"}, XType: boolType}
return
}
}
})
return typ, decl
}