summary refs log tree commit diff stats
path: root/compiler/ccgutils.nim
diff options
context:
space:
mode:
Diffstat (limited to 'compiler/ccgutils.nim')
-rw-r--r--compiler/ccgutils.nim122
1 files changed, 62 insertions, 60 deletions
diff --git a/compiler/ccgutils.nim b/compiler/ccgutils.nim
index 7396c0bf8..4e94c1867 100644
--- a/compiler/ccgutils.nim
+++ b/compiler/ccgutils.nim
@@ -9,31 +9,31 @@
 
 # This module declares some helpers for the C code generator.
 
-import 
-  ast, astalgo, ropes, lists, hashes, strutils, types, msgs, wordrecg, 
+import
+  ast, astalgo, ropes, lists, hashes, strutils, types, msgs, wordrecg,
   platform, trees
 
 proc getPragmaStmt*(n: PNode, w: TSpecialWord): PNode =
   case n.kind
-  of nkStmtList: 
-    for i in 0 .. < n.len: 
+  of nkStmtList:
+    for i in 0 .. < n.len:
       result = getPragmaStmt(n[i], w)
       if result != nil: break
   of nkPragma:
-    for i in 0 .. < n.len: 
+    for i in 0 .. < n.len:
       if whichPragma(n[i]) == w: return n[i]
   else: discard
 
 proc stmtsContainPragma*(n: PNode, w: TSpecialWord): bool =
   result = getPragmaStmt(n, w) != nil
 
-proc hashString*(s: string): BiggestInt = 
+proc hashString*(s: string): BiggestInt =
   # has to be the same algorithm as system.hashString!
-  if CPU[targetCPU].bit == 64: 
+  if CPU[targetCPU].bit == 64:
     # we have to use the same bitwidth
     # as the target CPU
     var b = 0'i64
-    for i in countup(0, len(s) - 1): 
+    for i in countup(0, len(s) - 1):
       b = b +% ord(s[i])
       b = b +% `shl`(b, 10)
       b = b xor `shr`(b, 6)
@@ -41,9 +41,9 @@ proc hashString*(s: string): BiggestInt =
     b = b xor `shr`(b, 11)
     b = b +% `shl`(b, 15)
     result = b
-  else: 
+  else:
     var a = 0'i32
-    for i in countup(0, len(s) - 1): 
+    for i in countup(0, len(s) - 1):
       a = a +% ord(s[i]).int32
       a = a +% `shl`(a, 10'i32)
       a = a xor `shr`(a, 6'i32)
@@ -52,11 +52,11 @@ proc hashString*(s: string): BiggestInt =
     a = a +% `shl`(a, 15'i32)
     result = a
 
-var 
+var
   gTypeTable: array[TTypeKind, TIdTable]
   gCanonicalTypes: array[TTypeKind, PType]
 
-proc initTypeTables() = 
+proc initTypeTables() =
   for i in countup(low(TTypeKind), high(TTypeKind)): initIdTable(gTypeTable[i])
 
 proc resetCaches* =
@@ -67,28 +67,39 @@ proc resetCaches* =
 
 when false:
   proc echoStats*() =
-    for i in countup(low(TTypeKind), high(TTypeKind)): 
+    for i in countup(low(TTypeKind), high(TTypeKind)):
       echo i, " ", gTypeTable[i].counter
-  
-proc getUniqueType*(key: PType): PType = 
+
+proc slowSearch(key: PType; k: TTypeKind): PType =
+  # tuples are quite horrible as C does not support them directly and
+  # tuple[string, string] is a (strange) subtype of
+  # tuple[nameA, nameB: string]. This bites us here, so we
+  # use 'sameBackendType' instead of 'sameType'.
+  if idTableHasObjectAsKey(gTypeTable[k], key): return key
+  for h in countup(0, high(gTypeTable[k].data)):
+    var t = PType(gTypeTable[k].data[h].key)
+    if t != nil and sameBackendType(t, key):
+      return t
+  idTablePut(gTypeTable[k], key, key)
+  result = key
+
+proc getUniqueType*(key: PType): PType =
   # this is a hotspot in the compiler!
-  if key == nil: return 
+  if key == nil: return
   var k = key.kind
   case k
-  of  tyBool, tyChar, 
-      tyInt..tyUInt64:
+  of tyBool, tyChar, tyInt..tyUInt64:
     # no canonicalization for integral types, so that e.g. ``pid_t`` is
     # produced instead of ``NI``.
     result = key
-  of  tyEmpty, tyNil, tyExpr, tyStmt, tyPointer, tyString, 
+  of  tyEmpty, tyNil, tyExpr, tyStmt, tyPointer, tyString,
       tyCString, tyNone, tyBigNum:
     result = gCanonicalTypes[k]
     if result == nil:
       gCanonicalTypes[k] = key
       result = key
-  of tyTypeDesc, tyTypeClasses, tyGenericParam,
-     tyFromExpr, tyFieldAccessor:
-    internalError("GetUniqueType")
+  of tyTypeDesc, tyTypeClasses, tyGenericParam, tyFromExpr, tyFieldAccessor:
+    internalError("getUniqueType")
   of tyDistinct:
     if key.deepCopy != nil: result = key
     else: result = getUniqueType(lastSon(key))
@@ -98,42 +109,39 @@ proc getUniqueType*(key: PType): PType =
     #if obj.sym != nil and obj.sym.name.s == "TOption":
     #  echo "for ", typeToString(key), " I returned "
     #  debug result
-  of tyArrayConstr, tyGenericInvokation, tyGenericBody,
+  of tyPtr, tyRef, tyVar:
+    let elemType = lastSon(key)
+    if elemType.kind in {tyBool, tyChar, tyInt..tyUInt64}:
+      # no canonicalization for integral types, so that e.g. ``ptr pid_t`` is
+      # produced instead of ``ptr NI``.
+      result = key
+    else:
+      result = slowSearch(key, k)
+  of tyArrayConstr, tyGenericInvocation, tyGenericBody,
      tyOpenArray, tyArray, tySet, tyRange, tyTuple,
-     tyPtr, tyRef, tySequence, tyForward, tyVarargs, tyProxy, tyVar:
-    # tuples are quite horrible as C does not support them directly and
-    # tuple[string, string] is a (strange) subtype of
-    # tuple[nameA, nameB: string]. This bites us here, so we 
-    # use 'sameBackendType' instead of 'sameType'.
-
+     tySequence, tyForward, tyVarargs, tyProxy:
     # we have to do a slow linear search because types may need
     # to be compared by their structure:
-    if idTableHasObjectAsKey(gTypeTable[k], key): return key 
-    for h in countup(0, high(gTypeTable[k].data)): 
-      var t = PType(gTypeTable[k].data[h].key)
-      if t != nil and sameBackendType(t, key): 
-        return t
-    idTablePut(gTypeTable[k], key, key)
-    result = key
+    result = slowSearch(key, k)
   of tyObject:
     if tfFromGeneric notin key.flags:
       # fast case; lookup per id suffices:
       result = PType(idTableGet(gTypeTable[k], key))
-      if result == nil: 
+      if result == nil:
         idTablePut(gTypeTable[k], key, key)
         result = key
     else:
       # ugly slow case: need to compare by structure
       if idTableHasObjectAsKey(gTypeTable[k], key): return key
-      for h in countup(0, high(gTypeTable[k].data)): 
+      for h in countup(0, high(gTypeTable[k].data)):
         var t = PType(gTypeTable[k].data[h].key)
-        if t != nil and sameType(t, key): 
+        if t != nil and sameBackendType(t, key):
           return t
       idTablePut(gTypeTable[k], key, key)
-      result = key    
+      result = key
   of tyEnum:
     result = PType(idTableGet(gTypeTable[k], key))
-    if result == nil: 
+    if result == nil:
       idTablePut(gTypeTable[k], key, key)
       result = key
   of tyProc:
@@ -141,24 +149,18 @@ proc getUniqueType*(key: PType): PType =
       result = key
     else:
       # ugh, we need the canon here:
-      if idTableHasObjectAsKey(gTypeTable[k], key): return key 
-      for h in countup(0, high(gTypeTable[k].data)): 
-        var t = PType(gTypeTable[k].data[h].key)
-        if t != nil and sameBackendType(t, key): 
-          return t
-      idTablePut(gTypeTable[k], key, key)
-      result = key
-      
-proc tableGetType*(tab: TIdTable, key: PType): RootRef = 
+      result = slowSearch(key, k)
+
+proc tableGetType*(tab: TIdTable, key: PType): RootRef =
   # returns nil if we need to declare this type
   result = idTableGet(tab, key)
-  if (result == nil) and (tab.counter > 0): 
+  if (result == nil) and (tab.counter > 0):
     # we have to do a slow linear search because types may need
     # to be compared by their structure:
-    for h in countup(0, high(tab.data)): 
+    for h in countup(0, high(tab.data)):
       var t = PType(tab.data[h].key)
-      if t != nil: 
-        if sameType(t, key): 
+      if t != nil:
+        if sameType(t, key):
           return tab.data[h].val
 
 proc makeSingleLineCString*(s: string): string =
@@ -191,20 +193,20 @@ proc mangle*(name: string): string =
     else:
       add(result, "HEX" & toHex(ord(c), 2))
 
-proc makeLLVMString*(s: string): PRope = 
+proc makeLLVMString*(s: string): Rope =
   const MaxLineLength = 64
   result = nil
   var res = "c\""
-  for i in countup(0, len(s) - 1): 
-    if (i + 1) mod MaxLineLength == 0: 
-      app(result, toRope(res))
+  for i in countup(0, len(s) - 1):
+    if (i + 1) mod MaxLineLength == 0:
+      add(result, rope(res))
       setLen(res, 0)
     case s[i]
-    of '\0'..'\x1F', '\x80'..'\xFF', '\"', '\\': 
+    of '\0'..'\x1F', '\x80'..'\xFF', '\"', '\\':
       add(res, '\\')
       add(res, toHex(ord(s[i]), 2))
     else: add(res, s[i])
   add(res, "\\00\"")
-  app(result, toRope(res))
+  add(result, rope(res))
 
 initTypeTables()
ass='oid'>9e8c8f473 ^
11b695875 ^
d46407caf ^
1785c6877 ^
07d5a8085 ^
8b2a9401a ^
07d5a8085 ^








405b86068
07d5a8085 ^
9e8c8f473 ^
a4559ab17 ^
cf68d926d ^
a4559ab17 ^

9e8c8f473 ^
a4559ab17 ^
d46407caf ^
405b86068

fcfaf2a84 ^
a4559ab17 ^
fcfaf2a84 ^
a4559ab17 ^
405b86068


a4559ab17 ^
405b86068
66a7e3d37 ^
9e8c8f473 ^
66a7e3d37 ^



405b86068
d46407caf ^
9e8c8f473 ^
e792940f5 ^


4ea5f3e6e ^
1785c6877 ^
0ef7d802c ^
6165e8498 ^
9e8c8f473 ^
0ef7d802c ^
6165e8498 ^
9e8c8f473 ^
d46407caf ^
0ef7d802c ^
6165e8498 ^
9e8c8f473 ^
0ef7d802c ^
6165e8498 ^
9e8c8f473 ^
35690dc37 ^
9e8c8f473 ^
0ef7d802c ^
6165e8498 ^
9e8c8f473 ^
d46407caf ^
35690dc37 ^


07d5a8085 ^





d46407caf ^
0ef7d802c ^
35690dc37 ^

d46407caf ^
0ef7d802c ^
6165e8498 ^
9e8c8f473 ^
0ef7d802c ^
6165e8498 ^
9e8c8f473 ^
0ef7d802c ^
6165e8498 ^
9e8c8f473 ^
0ef7d802c ^
35690dc37 ^
07d5a8085 ^



d46407caf ^
0ef7d802c ^
6165e8498 ^
9e8c8f473 ^
0ef7d802c ^

6165e8498 ^
9e8c8f473 ^
0ef7d802c ^

35690dc37 ^
c3e6fb0e3 ^

d46407caf ^
0ef7d802c ^
6165e8498 ^
9e8c8f473 ^
0ef7d802c ^
6165e8498 ^
9e8c8f473 ^
0ef7d802c ^

6165e8498 ^
9e8c8f473 ^
0ef7d802c ^
6165e8498 ^
9e8c8f473 ^
d46407caf ^
0ef7d802c ^
6165e8498 ^
07d5a8085 ^
35690dc37 ^
0ef7d802c ^
6165e8498 ^
c0c4a7d6a ^
0ef7d802c ^
6165e8498 ^
c0c4a7d6a ^
d46407caf ^
0ef7d802c ^
6165e8498 ^
c0c4a7d6a ^
0ef7d802c ^
6165e8498 ^
c0c4a7d6a ^
d46407caf ^
0ef7d802c ^
6165e8498 ^
9e8c8f473 ^



0ef7d802c ^

6165e8498 ^
9e8c8f473 ^



0ef7d802c ^

6165e8498 ^
9e8c8f473 ^



f45a2f23b ^
0ef7d802c ^
6165e8498 ^
9e8c8f473 ^



2900ceae3 ^
52851b722 ^
35690dc37 ^
6165e8498 ^
35690dc37 ^
6165e8498 ^
d46407caf ^
35690dc37 ^
6165e8498 ^
35690dc37 ^
6165e8498 ^
35690dc37 ^
6165e8498 ^
07d5a8085 ^
35690dc37 ^
6165e8498 ^
07d5a8085 ^
d46407caf ^
35690dc37 ^
6165e8498 ^
35690dc37 ^

07d5a8085 ^
e1a82987a ^
07d5a8085 ^



5b28d0820 ^
07d5a8085 ^


35690dc37 ^
6165e8498 ^
35690dc37 ^
6165e8498 ^
35690dc37 ^
6165e8498 ^
35690dc37 ^
6165e8498 ^
35690dc37 ^

6165e8498 ^
35690dc37 ^
6165e8498 ^
35690dc37 ^
6d1532439 ^
35690dc37 ^
6165e8498 ^
35690dc37 ^
6165e8498 ^

07d5a8085 ^

405b86068
4ea5f3e6e ^

bfcbe6477 ^
edbd191f7 ^
bfcbe6477 ^
edbd191f7 ^
bfcbe6477 ^
edbd191f7 ^
bfcbe6477 ^
edbd191f7 ^
35690dc37 ^
67b3c4b31 ^


d46407caf ^
67b3c4b31 ^

2900ceae3 ^

405b86068

5aced9186 ^
1ad1b93f0 ^






22789a827 ^
1ad1b93f0 ^


22789a827 ^

1ad1b93f0 ^



9723e3064 ^


43bc72f1f ^




1ad1b93f0 ^




1785c6877 ^
4ea5f3e6e ^


d46407caf ^
c0c4a7d6a ^
2fbdf9320 ^
c0c4a7d6a ^


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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359