summary refs log tree commit diff stats
path: root/compiler/lambdalifting.nim
Commit message (Collapse)AuthorAgeFilesLines
* fixes nested gensym'ed parameters; fixes #9476Araq2018-12-081-4/+1
|
* fixes #9441Araq2018-12-061-2/+20
|
* lambdalifting: be consistent in its handling for 'func'Araq2018-12-051-9/+7
|
* Change the order of compilation passes, transformation is made lazy at code ↵cooldome2018-10-181-6/+11
| | | | | | | | | | | | | | | gen (#8489) * Ast no transformation * Add getImplNoTransform to the macros module * progress on delaying transf * Fix methods tranformation * Fix lazy lambdalifting * fix create thread wrapper * transform for lambda lifting * improve getImplTransformed * Fix destructor tests * try to fix nimprof for linux
* improve error message, closes #2361 (#9387)Miran2018-10-171-2/+3
| | | | | | * improve error message * use single % operation
* Access implicit `result` trough envP in closures (#8471)LemonBoy2018-07-301-1/+13
| | | | | | | Reuse the existing machinery, most of the changes are only needed to handle the `result = result` node in nkReturnStmt produced by the closure iterator transform. Fixes #338
* fixes yet another merge conflictAraq2018-06-111-86/+2
|\
| * Removed oldIterTranf featureYuriy Glukhov2018-06-101-86/+2
| |
* | fixex merge conflictsAraq2018-06-081-21/+41
|\|
| * Merge branch 'devel' into yield-in-tryYuriy Glukhov2018-05-151-102/+101
| |\
| * | CosmeticsYuriy Glukhov2018-05-091-2/+2
| | |
| * | Small fix and cosmeticsYuriy Glukhov2018-05-091-2/+1
| | |
| * | Closure iter transformationYuriy Glukhov2018-05-091-18/+42
| | |
* | | baby steps for incremental compilationAndreas Rumpf2018-05-301-1/+1
| | |
* | | refactoring: remove idents.legacy global variable and pass the IdentCache ↵Andreas Rumpf2018-05-271-20/+20
| | | | | | | | | | | | around explicitly
* | | remove more global variables in the Nim compilerAndreas Rumpf2018-05-271-1/+1
| | |
* | | make tests green againAndreas Rumpf2018-05-181-1/+1
| | |
* | | remove ast.emptyNode global; cleanup configuration.nimAraq2018-05-161-2/+2
| |/ |/|
* | options.nim: no global variables anymoreAndreas Rumpf2018-05-131-13/+10
| |
* | lambda lifting compiles againAndreas Rumpf2018-05-121-85/+90
|/
* Remove PHP backend. (#7606)treeform2018-04-181-2/+2
| | | | | | | | | | * remove php * fix * keep nimphpext * remove targetJS enum
* Fix some usages of typedesc in async procsZahary Karadjov2018-04-131-1/+2
| | | | | This also fixes a compilation error in modules, based only on the new async module (i.e. not importing the full asyncdispatch)
* better error messages for illegal capturesAraq2018-03-191-2/+6
|
* preparations for language extensions: 'sink' and 'lent' typesAndreas Rumpf2018-01-071-1/+1
|
* first steps in adding template/macro calls to stack tracesAraq2017-12-211-1/+1
|
* fixes #6626Araq2017-12-151-0/+13
|
* bugfix: detect captures in non-closure inner procsAraq2017-11-031-1/+4
|
* first implementation of the 'func' keywordAndreas Rumpf2017-09-231-4/+5
|
* closures have an object type field so that we can perform correct deepCopy() ↵Andreas Rumpf2017-07-221-1/+1
| | | | for the BoehmGC target
* removed compiler internal list implementation (#5371)Arne Döring2017-02-221-1/+1
|
* simple closure iterator calling closure iterator works; refs #3837Andreas Rumpf2016-12-271-9/+15
|
* closure types get names; refs #4332Araq2016-11-291-16/+17
|
* minor steps to make closure iterators work with the JS backendAndreas Rumpf2016-09-121-0/+7
|
* fixes #4070Andreas Rumpf2016-05-111-0/+1
|
* fixes #3995Andreas Rumpf2016-04-031-0/+4
|
* no LL for PHPAndreas Rumpf2016-02-061-1/+2
|
* fixes #3730Araq2016-01-181-2/+6
|
* fixes yet another LL regression (Aporia compiles again)Andreas Rumpf2016-01-181-1/+1
|
* fixes iterator codegen regressionAndreas Rumpf2016-01-141-0/+3
|
* fixes yet another regressionAndreas Rumpf2016-01-131-3/+7
|
* fixes more regressionsAndreas Rumpf2016-01-131-1/+15
|
* fixes async regressionAndreas Rumpf2016-01-111-2/+5
|
* fixes all known regressionsAndreas Rumpf2016-01-061-1/+1
|
* fixes #2007Andreas Rumpf2016-01-051-2/+2
|
* minor bugfixAndreas Rumpf2016-01-041-3/+3
|
* fixes #2659Andreas Rumpf2016-01-041-1/+1
|
* fixes #3201, fixes #2766Andreas Rumpf2016-01-041-1/+2
|
* fixes #3636Andreas Rumpf2016-01-041-8/+13
|
* fixes #1725Andreas Rumpf2016-01-041-7/+12
|
* async works againAndreas Rumpf2016-01-031-20/+28
|
ersion of signature hashing for better incremental builds' href='/ahoang/Nim/commit/compiler/sighashes.nim?h=devel&id=3b43cff0f8ed5f3bd4026c92d3882e8e9c069c68'>3b43cff0f ^
3b43cff0f ^
860cbd310 ^
3b43cff0f ^








54a6c5b34 ^
b234b082b ^

3b43cff0f ^


860cbd310 ^
3b43cff0f ^








70ea45cdb ^
3b43cff0f ^
860cbd310 ^


bafc94a49 ^
3b43cff0f ^
70ea45cdb ^
860cbd310 ^
3b43cff0f ^
b234b082b ^




d2cbf7f24 ^







b234b082b ^
860cbd310 ^
72af7e682 ^

4104ee121 ^
860cbd310 ^
b234b082b ^


72af7e682 ^



860cbd310 ^
72af7e682 ^







04b91b0d3 ^

49667798e ^


42ae2064d ^
860cbd310 ^
72af7e682 ^
71a23fbbb ^








06a8d37f9 ^




71a23fbbb ^

72af7e682 ^










860cbd310 ^
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


                            
                                         






                                                                  
               

                         
                                          
                                         
 












                                                                               
                                 








                                                                           
 


                                                  
                  



















                                                                   
 



                                                                         

                                                        
 


                                                         







                                                            






                                                                    
 
                                          








                                                   












                                                                  
                                            





















                                                                            
                                              
 
                                                                      



               
             


                                        
          





                                 
                                               

                               

           
                   
             




                                                                       
                      
                         

                                                   
                                            

                                                                               
                    



                                                          



                             


                                                                        

                                                                                 





                                   
                                
         
               

                                      
                                        
                               
                                              
                
                   
             
                                         








                                                                      
                       

                           


                                                              
                                      








                                    
                                                      
                         


                                                        
                                            
       
                                                    
                                                               
 




                                                                    







                                                                                 
 
                                                                        

                   
                                   
                              


                                                                           



                                  
                             







                                       

                             


                                                                             
                   
                              
 








                                     




                                                                   

                              










                                       
                              
#
#
#           The Nim Compiler
#        (c) Copyright 2017 Andreas Rumpf
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

## Computes hash values for routine (proc, method etc) signatures.

import ast, md5
from hashes import Hash
from astalgo import debug
from types import typeToString, preferDesc
from strutils import startsWith, contains

when false:
  type
    SigHash* = uint32  ## a hash good enough for a filename or a proc signature

  proc sdbmHash(hash: SigHash, c: char): SigHash {.inline.} =
    return SigHash(c) + (hash shl 6) + (hash shl 16) - hash

  template `&=`*(x: var SigHash, c: char) = x = sdbmHash(x, c)
  template `&=`*(x: var SigHash, s: string) =
    for c in s: x = sdbmHash(x, c)

else:
  type
    SigHash* = distinct Md5Digest

  const
    cb64 = [
      "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N",
      "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
      "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n",
      "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
      "0", "1", "2", "3", "4", "5", "6", "7", "8", "9a",
      "9b", "9c"]

  proc toBase64a(s: cstring, len: int): string =
    ## encodes `s` into base64 representation.
    result = newStringOfCap(((len + 2) div 3) * 4)
    result.add '_'
    var i = 0
    while i < len - 2:
      let a = ord(s[i])
      let b = ord(s[i+1])
      let c = ord(s[i+2])
      result.add cb64[a shr 2]
      result.add cb64[((a and 3) shl 4) or ((b and 0xF0) shr 4)]
      result.add cb64[((b and 0x0F) shl 2) or ((c and 0xC0) shr 6)]
      result.add cb64[c and 0x3F]
      inc(i, 3)
    if i < len-1:
      let a = ord(s[i])
      let b = ord(s[i+1])
      result.add cb64[a shr 2]
      result.add cb64[((a and 3) shl 4) or ((b and 0xF0) shr 4)]
      result.add cb64[((b and 0x0F) shl 2)]
    elif i < len:
      let a = ord(s[i])
      result.add cb64[a shr 2]
      result.add cb64[(a and 3) shl 4]

  proc `$`*(u: SigHash): string =
    toBase64a(cast[cstring](unsafeAddr u), sizeof(u))
  proc `&=`(c: var MD5Context, s: string) = md5Update(c, s, s.len)
  proc `&=`(c: var MD5Context, ch: char) = md5Update(c, unsafeAddr ch, 1)
  proc `&=`(c: var MD5Context, i: BiggestInt) =
    md5Update(c, cast[cstring](unsafeAddr i), sizeof(i))

  template lowlevel(v) =
    md5Update(c, cast[cstring](unsafeAddr(v)), sizeof(v))

  proc `==`*(a, b: SigHash): bool =
    # {.borrow.}
    result = equalMem(unsafeAddr a, unsafeAddr b, sizeof(a))

  proc hash*(u: SigHash): Hash =
    result = 0
    for x in 0..3:
      result = (result shl 8) or u.MD5Digest[x].int
type
  ConsiderFlag* = enum
    CoProc
    CoType
    CoOwnerSig

proc hashType(c: var MD5Context, t: PType; flags: set[ConsiderFlag])

proc hashSym(c: var MD5Context, s: PSym) =
  if sfAnon in s.flags or s.kind == skGenericParam:
    c &= ":anon"
  else:
    var it = s
    while it != nil:
      c &= it.name.s
      c &= "."
      it = it.owner

proc hashTypeSym(c: var MD5Context, s: PSym) =
  if sfAnon in s.flags or s.kind == skGenericParam:
    c &= ":anon"
  else:
    var it = s
    while it != nil:
      if sfFromGeneric in it.flags and it.kind in routineKinds and
          it.typ != nil:
        hashType c, it.typ, {CoProc}
      c &= it.name.s
      c &= "."
      it = it.owner

proc hashTree(c: var MD5Context, n: PNode) =
  if n == nil:
    c &= "\255"
    return
  let k = n.kind
  c &= char(k)
  # we really must not hash line information. 'n.typ' is debatable but
  # shouldn't be necessary for now and avoids potential infinite recursions.
  case n.kind
  of nkEmpty, nkNilLit, nkType: discard
  of nkIdent:
    c &= n.ident.s
  of nkSym:
    hashSym(c, n.sym)
  of nkCharLit..nkUInt64Lit:
    let v = n.intVal
    lowlevel v
  of nkFloatLit..nkFloat64Lit:
    let v = n.floatVal
    lowlevel v
  of nkStrLit..nkTripleStrLit:
    c &= n.strVal
  else:
    for i in 0..<n.len: hashTree(c, n.sons[i])

proc hashType(c: var MD5Context, t: PType; flags: set[ConsiderFlag]) =
  if t == nil:
    c &= "\254"
    return

  case t.kind
  of tyGenericInvocation:
    for i in countup(0, sonsLen(t) - 1):
      c.hashType t.sons[i], flags
    return
  of tyDistinct:
    if CoType in flags:
      c.hashType t.lastSon, flags
    else:
      c.hashSym(t.sym)
    return
  of tyAlias, tyGenericInst, tyUserTypeClasses:
    c.hashType t.lastSon, flags
    return
  else:
    discard
  c &= char(t.kind)
  case t.kind
  of tyBool, tyChar, tyInt..tyUInt64:
    # no canonicalization for integral types, so that e.g. ``pid_t`` is
    # produced instead of ``NI``:
    if t.sym != nil and {sfImportc, sfExportc} * t.sym.flags != {}:
      c.hashSym(t.sym)
  of tyObject, tyEnum:
    if t.typeInst != nil:
      assert t.typeInst.kind == tyGenericInst
      for i in countup(1, sonsLen(t.typeInst) - 2):
        c.hashType t.typeInst.sons[i], flags
    # Every cyclic type in Nim need to be constructed via some 't.sym', so this
    # is actually safe without an infinite recursion check:
    if t.sym != nil:
      #if "Future:" in t.sym.name.s and t.typeInst == nil:
      #  writeStackTrace()
      #  echo "yes ", t.sym.name.s
      #  #quit 1
      if CoOwnerSig in flags:
        c.hashTypeSym(t.sym)
      else:
        c.hashSym(t.sym)
      if sfAnon in t.sym.flags:
        # generated object names can be identical, so we need to
        # disambiguate furthermore by hashing the field types and names:
        # mild hack to prevent endless recursions (makes nimforum compile again):
        excl t.sym.flags, sfAnon
        let n = t.n
        for i in 0 ..< n.len:
          assert n[i].kind == nkSym
          let s = n[i].sym
          c.hashSym s
          c.hashType s.typ, flags
        incl t.sym.flags, sfAnon
    else:
      c &= t.id
    if t.len > 0 and t.sons[0] != nil:
      hashType c, t.sons[0], flags
  of tyRef, tyPtr, tyGenericBody, tyVar:
    c.hashType t.lastSon, flags
    if tfVarIsPtr in t.flags: c &= ".varisptr"
  of tyFromExpr:
    c.hashTree(t.n)
  of tyTuple:
    if t.n != nil and CoType notin flags:
      assert(sonsLen(t.n) == sonsLen(t))
      for i in countup(0, sonsLen(t.n) - 1):
        assert(t.n.sons[i].kind == nkSym)
        c &= t.n.sons[i].sym.name.s
        c &= ':'
        c.hashType(t.sons[i], flags)
        c &= ','
    else:
      for i in countup(0, sonsLen(t) - 1): c.hashType t.sons[i], flags
  of tyRange, tyStatic:
    #if CoType notin flags:
    c.hashTree(t.n)
    c.hashType(t.sons[0], flags)
  of tyProc:
    c &= (if tfIterator in t.flags: "iterator " else: "proc ")
    if CoProc in flags and t.n != nil:
      let params = t.n
      for i in 1..<params.len:
        let param = params[i].sym
        c &= param.name.s
        c &= ':'
        c.hashType(param.typ, flags)
        c &= ','
      c.hashType(t.sons[0], flags)
    else:
      for i in 0..<t.len: c.hashType(t.sons[i], flags)
    c &= char(t.callConv)
    if CoType notin flags:
      if tfNoSideEffect in t.flags: c &= ".noSideEffect"
      if tfThread in t.flags: c &= ".thread"
    if tfVarargs in t.flags: c &= ".varargs"
  else:
    for i in 0..<t.len: c.hashType(t.sons[i], flags)
  if tfNotNil in t.flags and CoType notin flags: c &= "not nil"

when defined(debugSigHashes):
  import db_sqlite

  let db = open(connection="sighashes.db", user="araq", password="",
                database="sighashes")
  db.exec(sql"DROP TABLE IF EXISTS sighashes")
  db.exec sql"""CREATE TABLE sighashes(
    id integer primary key,
    hash varchar(5000) not null,
    type varchar(5000) not null,
    unique (hash, type))"""
  #  select hash, type from sighashes where hash in
  # (select hash from sighashes group by hash having count(*) > 1) order by hash;

proc hashType*(t: PType; flags: set[ConsiderFlag] = {CoType}): SigHash =
  var c: MD5Context
  md5Init c
  hashType c, t, flags+{CoOwnerSig}
  md5Final c, result.Md5Digest
  when defined(debugSigHashes):
    db.exec(sql"INSERT OR IGNORE INTO sighashes(type, hash) VALUES (?, ?)",
            typeToString(t), $result)

proc hashProc*(s: PSym): SigHash =
  var c: MD5Context
  md5Init c
  hashType c, s.typ, {CoProc}

  var m = s
  while m.kind != skModule: m = m.owner
  let p = m.owner
  assert p.kind == skPackage
  c &= p.name.s
  c &= "."
  c &= m.name.s
  if sfDispatcher in s.flags:
    c &= ".dispatcher"
  # so that createThread[void]() (aka generic specialization) gets a unique
  # hash, we also hash the line information. This is pretty bad, but the best
  # solution for now:
  #c &= s.info.line
  md5Final c, result.Md5Digest

proc hashNonProc*(s: PSym): SigHash =
  var c: MD5Context
  md5Init c
  hashSym(c, s)
  var it = s
  while it != nil:
    c &= it.name.s
    c &= "."
    it = it.owner
  # for bug #5135 we also take the position into account, but only
  # for parameters, because who knows what else position dependency
  # might cause:
  if s.kind == skParam:
    c &= s.position
  md5Final c, result.Md5Digest

proc hashOwner*(s: PSym): SigHash =
  var c: MD5Context
  md5Init c
  var m = s
  while m.kind != skModule: m = m.owner
  let p = m.owner
  assert p.kind == skPackage
  c &= p.name.s
  c &= "."
  c &= m.name.s

  md5Final c, result.Md5Digest