jsgen.nim 104 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2015 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. # This is the JavaScript code generator.
  10. discard """
  11. The JS code generator contains only 2 tricks:
  12. Trick 1
  13. -------
  14. Some locations (for example 'var int') require "fat pointers" (`etyBaseIndex`)
  15. which are pairs (array, index). The derefence operation is then 'array[index]'.
  16. Check `mapType` for the details.
  17. Trick 2
  18. -------
  19. It is preferable to generate '||' and '&&' if possible since that is more
  20. idiomatic and hence should be friendlier for the JS JIT implementation. However
  21. code like `foo and (let bar = baz())` cannot be translated this way. Instead
  22. the expressions need to be transformed into statements. `isSimpleExpr`
  23. implements the required case distinction.
  24. """
  25. import
  26. ast, trees, magicsys, options,
  27. nversion, msgs, idents, types,
  28. ropes, ccgutils, wordrecg, renderer,
  29. cgmeth, lowerings, sighashes, modulegraphs, lineinfos,
  30. transf, injectdestructors, sourcemap, astmsgs, backendpragmas
  31. import pipelineutils
  32. import json, sets, math, tables, intsets
  33. import strutils except addf
  34. when defined(nimPreviewSlimSystem):
  35. import std/[assertions, syncio]
  36. import std/formatfloat
  37. type
  38. TJSGen = object of PPassContext
  39. module: PSym
  40. graph: ModuleGraph
  41. config: ConfigRef
  42. sigConflicts: CountTable[SigHash]
  43. initProc: PProc
  44. BModule = ref TJSGen
  45. TJSTypeKind = enum # necessary JS "types"
  46. etyNone, # no type
  47. etyNull, # null type
  48. etyProc, # proc type
  49. etyBool, # bool type
  50. etySeq, # Nim seq or string type
  51. etyInt, # JavaScript's int
  52. etyFloat, # JavaScript's float
  53. etyString, # JavaScript's string
  54. etyObject, # JavaScript's reference to an object
  55. etyBaseIndex # base + index needed
  56. TResKind = enum
  57. resNone, # not set
  58. resExpr, # is some complex expression
  59. resVal, # is a temporary/value/l-value
  60. resCallee # expression is callee
  61. TCompRes = object
  62. kind: TResKind
  63. typ: TJSTypeKind
  64. res: Rope # result part; index if this is an
  65. # (address, index)-tuple
  66. address: Rope # address of an (address, index)-tuple
  67. tmpLoc: Rope # tmp var which stores the (address, index)
  68. # pair to prevent multiple evals.
  69. # the tmp is initialized upon evaling the
  70. # address.
  71. # might be nil.
  72. # (see `maybeMakeTemp`)
  73. TBlock = object
  74. id: int # the ID of the label; positive means that it
  75. # has been used (i.e. the label should be emitted)
  76. isLoop: bool # whether it's a 'block' or 'while'
  77. PGlobals = ref object of RootObj
  78. typeInfo, constants, code: Rope
  79. forwarded: seq[PSym]
  80. generatedSyms: IntSet
  81. typeInfoGenerated: IntSet
  82. unique: int # for temp identifier generation
  83. inSystem: bool
  84. PProc = ref TProc
  85. TProc = object
  86. procDef: PNode
  87. prc: PSym
  88. globals, locals, body: Rope
  89. options: TOptions
  90. optionsStack: seq[TOptions]
  91. module: BModule
  92. g: PGlobals
  93. generatedParamCopies: IntSet
  94. beforeRetNeeded: bool
  95. unique: int # for temp identifier generation
  96. blocks: seq[TBlock]
  97. extraIndent: int
  98. up: PProc # up the call chain; required for closure support
  99. declaredGlobals: IntSet
  100. previousFileName: string # For frameInfo inside templates.
  101. template config*(p: PProc): ConfigRef = p.module.config
  102. proc indentLine(p: PProc, r: Rope): Rope =
  103. var p = p
  104. var ind = 0
  105. while true:
  106. inc ind, p.blocks.len + p.extraIndent
  107. if p.up == nil or p.up.prc != p.prc.owner:
  108. break
  109. p = p.up
  110. result = repeat(' ', ind*2) & r
  111. template line(p: PProc, added: string) =
  112. p.body.add(indentLine(p, rope(added)))
  113. template lineF(p: PProc, frmt: FormatStr, args: varargs[Rope]) =
  114. p.body.add(indentLine(p, ropes.`%`(frmt, args)))
  115. template nested(p, body) =
  116. inc p.extraIndent
  117. body
  118. dec p.extraIndent
  119. proc newGlobals(): PGlobals =
  120. result = PGlobals(forwarded: @[],
  121. generatedSyms: initIntSet(),
  122. typeInfoGenerated: initIntSet()
  123. )
  124. proc initCompRes(): TCompRes =
  125. result = TCompRes(address: "", res: "",
  126. tmpLoc: "", typ: etyNone, kind: resNone
  127. )
  128. proc rdLoc(a: TCompRes): Rope {.inline.} =
  129. if a.typ != etyBaseIndex:
  130. result = a.res
  131. else:
  132. result = "$1[$2]" % [a.address, a.res]
  133. proc newProc(globals: PGlobals, module: BModule, procDef: PNode,
  134. options: TOptions): PProc =
  135. result = PProc(
  136. blocks: @[],
  137. optionsStack: if module.initProc != nil: module.initProc.optionsStack
  138. else: @[],
  139. options: options,
  140. module: module,
  141. procDef: procDef,
  142. g: globals,
  143. extraIndent: int(procDef != nil))
  144. if procDef != nil: result.prc = procDef[namePos].sym
  145. proc initProcOptions(module: BModule): TOptions =
  146. result = module.config.options
  147. if PGlobals(module.graph.backend).inSystem:
  148. result.excl(optStackTrace)
  149. proc newInitProc(globals: PGlobals, module: BModule): PProc =
  150. result = newProc(globals, module, nil, initProcOptions(module))
  151. proc declareGlobal(p: PProc; id: int; r: Rope) =
  152. if p.prc != nil and not p.declaredGlobals.containsOrIncl(id):
  153. p.locals.addf("global $1;$n", [r])
  154. const
  155. MappedToObject = {tyObject, tyArray, tyTuple, tyOpenArray,
  156. tySet, tyVarargs}
  157. proc mapType(typ: PType): TJSTypeKind =
  158. let t = skipTypes(typ, abstractInst)
  159. case t.kind
  160. of tyVar, tyRef, tyPtr:
  161. if skipTypes(t.lastSon, abstractInst).kind in MappedToObject:
  162. result = etyObject
  163. else:
  164. result = etyBaseIndex
  165. of tyPointer:
  166. # treat a tyPointer like a typed pointer to an array of bytes
  167. result = etyBaseIndex
  168. of tyRange, tyDistinct, tyOrdinal, tyProxy, tyLent:
  169. # tyLent is no-op as JS has pass-by-reference semantics
  170. result = mapType(t[0])
  171. of tyInt..tyInt64, tyUInt..tyUInt64, tyEnum, tyChar: result = etyInt
  172. of tyBool: result = etyBool
  173. of tyFloat..tyFloat128: result = etyFloat
  174. of tySet: result = etyObject # map a set to a table
  175. of tyString, tySequence: result = etySeq
  176. of tyObject, tyArray, tyTuple, tyOpenArray, tyVarargs, tyUncheckedArray:
  177. result = etyObject
  178. of tyNil: result = etyNull
  179. of tyGenericParam, tyGenericBody, tyGenericInvocation,
  180. tyNone, tyFromExpr, tyForward, tyEmpty,
  181. tyUntyped, tyTyped, tyTypeDesc, tyBuiltInTypeClass, tyCompositeTypeClass,
  182. tyAnd, tyOr, tyNot, tyAnything, tyVoid:
  183. result = etyNone
  184. of tyGenericInst, tyInferred, tyAlias, tyUserTypeClass, tyUserTypeClassInst,
  185. tySink, tyOwned:
  186. result = mapType(typ.lastSon)
  187. of tyStatic:
  188. if t.n != nil: result = mapType(lastSon t)
  189. else: result = etyNone
  190. of tyProc: result = etyProc
  191. of tyCstring: result = etyString
  192. of tyConcept, tyIterable:
  193. raiseAssert "unreachable"
  194. proc mapType(p: PProc; typ: PType): TJSTypeKind =
  195. result = mapType(typ)
  196. proc mangleName(m: BModule, s: PSym): Rope =
  197. proc validJsName(name: string): bool =
  198. result = true
  199. const reservedWords = ["abstract", "await", "boolean", "break", "byte",
  200. "case", "catch", "char", "class", "const", "continue", "debugger",
  201. "default", "delete", "do", "double", "else", "enum", "export", "extends",
  202. "false", "final", "finally", "float", "for", "function", "goto", "if",
  203. "implements", "import", "in", "instanceof", "int", "interface", "let",
  204. "long", "native", "new", "null", "package", "private", "protected",
  205. "public", "return", "short", "static", "super", "switch", "synchronized",
  206. "this", "throw", "throws", "transient", "true", "try", "typeof", "var",
  207. "void", "volatile", "while", "with", "yield"]
  208. case name
  209. of reservedWords:
  210. return false
  211. else:
  212. discard
  213. if name[0] in {'0'..'9'}: return false
  214. for chr in name:
  215. if chr notin {'A'..'Z','a'..'z','_','$','0'..'9'}:
  216. return false
  217. result = s.loc.r
  218. if result == "":
  219. if s.kind == skField and s.name.s.validJsName:
  220. result = rope(s.name.s)
  221. elif s.kind == skTemp:
  222. result = rope(mangle(s.name.s))
  223. else:
  224. var x = newStringOfCap(s.name.s.len)
  225. var i = 0
  226. while i < s.name.s.len:
  227. let c = s.name.s[i]
  228. case c
  229. of 'A'..'Z', 'a'..'z', '_', '0'..'9':
  230. x.add c
  231. else:
  232. x.add("HEX" & toHex(ord(c), 2))
  233. inc i
  234. result = rope(x)
  235. # From ES5 on reserved words can be used as object field names
  236. if s.kind != skField:
  237. if m.config.hcrOn:
  238. # When hot reloading is enabled, we must ensure that the names
  239. # of functions and types will be preserved across rebuilds:
  240. result.add(idOrSig(s, m.module.name.s, m.sigConflicts, m.config))
  241. else:
  242. result.add("_")
  243. result.add(rope(s.id))
  244. s.loc.r = result
  245. proc escapeJSString(s: string): string =
  246. result = newStringOfCap(s.len + s.len shr 2)
  247. result.add("\"")
  248. for c in items(s):
  249. case c
  250. of '\l': result.add("\\n")
  251. of '\r': result.add("\\r")
  252. of '\t': result.add("\\t")
  253. of '\b': result.add("\\b")
  254. of '\a': result.add("\\a")
  255. of '\e': result.add("\\e")
  256. of '\v': result.add("\\v")
  257. of '\\': result.add("\\\\")
  258. of '\"': result.add("\\\"")
  259. else: result.add(c)
  260. result.add("\"")
  261. proc makeJSString(s: string, escapeNonAscii = true): Rope =
  262. if escapeNonAscii:
  263. result = strutils.escape(s).rope
  264. else:
  265. result = escapeJSString(s).rope
  266. proc makeJsNimStrLit(s: string): Rope =
  267. var x = newStringOfCap(4*s.len+1)
  268. x.add "["
  269. var i = 0
  270. if i < s.len:
  271. x.addInt int64(s[i])
  272. inc i
  273. while i < s.len:
  274. x.add ","
  275. x.addInt int64(s[i])
  276. inc i
  277. x.add "]"
  278. result = rope(x)
  279. include jstypes
  280. proc gen(p: PProc, n: PNode, r: var TCompRes)
  281. proc genStmt(p: PProc, n: PNode)
  282. proc genProc(oldProc: PProc, prc: PSym): Rope
  283. proc genConstant(p: PProc, c: PSym)
  284. proc useMagic(p: PProc, name: string) =
  285. if name.len == 0: return
  286. var s = magicsys.getCompilerProc(p.module.graph, name)
  287. if s != nil:
  288. internalAssert p.config, s.kind in {skProc, skFunc, skMethod, skConverter}
  289. if not p.g.generatedSyms.containsOrIncl(s.id):
  290. let code = genProc(p, s)
  291. p.g.constants.add(code)
  292. else:
  293. if p.prc != nil:
  294. globalError(p.config, p.prc.info, "system module needs: " & name)
  295. else:
  296. rawMessage(p.config, errGenerated, "system module needs: " & name)
  297. proc isSimpleExpr(p: PProc; n: PNode): bool =
  298. # calls all the way down --> can stay expression based
  299. case n.kind
  300. of nkCallKinds, nkBracketExpr, nkDotExpr, nkPar, nkTupleConstr,
  301. nkObjConstr, nkBracket, nkCurly,
  302. nkDerefExpr, nkHiddenDeref, nkAddr, nkHiddenAddr,
  303. nkConv, nkHiddenStdConv, nkHiddenSubConv:
  304. for c in n:
  305. if not p.isSimpleExpr(c): return false
  306. result = true
  307. of nkStmtListExpr:
  308. for i in 0..<n.len-1:
  309. if n[i].kind notin {nkCommentStmt, nkEmpty}: return false
  310. result = isSimpleExpr(p, n.lastSon)
  311. else:
  312. result = n.isAtom
  313. proc getTemp(p: PProc, defineInLocals: bool = true): Rope =
  314. inc(p.unique)
  315. result = "Temporary$1" % [rope(p.unique)]
  316. if defineInLocals:
  317. p.locals.add(p.indentLine("var $1;$n" % [result]))
  318. proc genAnd(p: PProc, a, b: PNode, r: var TCompRes) =
  319. assert r.kind == resNone
  320. var x, y: TCompRes = default(TCompRes)
  321. if p.isSimpleExpr(a) and p.isSimpleExpr(b):
  322. gen(p, a, x)
  323. gen(p, b, y)
  324. r.kind = resExpr
  325. r.res = "($1 && $2)" % [x.rdLoc, y.rdLoc]
  326. else:
  327. r.res = p.getTemp
  328. r.kind = resVal
  329. # while a and b:
  330. # -->
  331. # while true:
  332. # aa
  333. # if not a: tmp = false
  334. # else:
  335. # bb
  336. # tmp = b
  337. # tmp
  338. gen(p, a, x)
  339. lineF(p, "if (!$1) $2 = false; else {", [x.rdLoc, r.rdLoc])
  340. p.nested:
  341. gen(p, b, y)
  342. lineF(p, "$2 = $1;", [y.rdLoc, r.rdLoc])
  343. line(p, "}")
  344. proc genOr(p: PProc, a, b: PNode, r: var TCompRes) =
  345. assert r.kind == resNone
  346. var x, y: TCompRes = default(TCompRes)
  347. if p.isSimpleExpr(a) and p.isSimpleExpr(b):
  348. gen(p, a, x)
  349. gen(p, b, y)
  350. r.kind = resExpr
  351. r.res = "($1 || $2)" % [x.rdLoc, y.rdLoc]
  352. else:
  353. r.res = p.getTemp
  354. r.kind = resVal
  355. gen(p, a, x)
  356. lineF(p, "if ($1) $2 = true; else {", [x.rdLoc, r.rdLoc])
  357. p.nested:
  358. gen(p, b, y)
  359. lineF(p, "$2 = $1;", [y.rdLoc, r.rdLoc])
  360. line(p, "}")
  361. type
  362. TMagicFrmt = array[0..1, string]
  363. TMagicOps = array[mAddI..mStrToStr, TMagicFrmt]
  364. const # magic checked op; magic unchecked op;
  365. jsMagics: TMagicOps = [
  366. mAddI: ["addInt", ""],
  367. mSubI: ["subInt", ""],
  368. mMulI: ["mulInt", ""],
  369. mDivI: ["divInt", ""],
  370. mModI: ["modInt", ""],
  371. mSucc: ["addInt", ""],
  372. mPred: ["subInt", ""],
  373. mAddF64: ["", ""],
  374. mSubF64: ["", ""],
  375. mMulF64: ["", ""],
  376. mDivF64: ["", ""],
  377. mShrI: ["", ""],
  378. mShlI: ["", ""],
  379. mAshrI: ["", ""],
  380. mBitandI: ["", ""],
  381. mBitorI: ["", ""],
  382. mBitxorI: ["", ""],
  383. mMinI: ["nimMin", "nimMin"],
  384. mMaxI: ["nimMax", "nimMax"],
  385. mAddU: ["", ""],
  386. mSubU: ["", ""],
  387. mMulU: ["", ""],
  388. mDivU: ["", ""],
  389. mModU: ["", ""],
  390. mEqI: ["", ""],
  391. mLeI: ["", ""],
  392. mLtI: ["", ""],
  393. mEqF64: ["", ""],
  394. mLeF64: ["", ""],
  395. mLtF64: ["", ""],
  396. mLeU: ["", ""],
  397. mLtU: ["", ""],
  398. mEqEnum: ["", ""],
  399. mLeEnum: ["", ""],
  400. mLtEnum: ["", ""],
  401. mEqCh: ["", ""],
  402. mLeCh: ["", ""],
  403. mLtCh: ["", ""],
  404. mEqB: ["", ""],
  405. mLeB: ["", ""],
  406. mLtB: ["", ""],
  407. mEqRef: ["", ""],
  408. mLePtr: ["", ""],
  409. mLtPtr: ["", ""],
  410. mXor: ["", ""],
  411. mEqCString: ["", ""],
  412. mEqProc: ["", ""],
  413. mUnaryMinusI: ["negInt", ""],
  414. mUnaryMinusI64: ["negInt64", ""],
  415. mAbsI: ["absInt", ""],
  416. mNot: ["", ""],
  417. mUnaryPlusI: ["", ""],
  418. mBitnotI: ["", ""],
  419. mUnaryPlusF64: ["", ""],
  420. mUnaryMinusF64: ["", ""],
  421. mCharToStr: ["nimCharToStr", "nimCharToStr"],
  422. mBoolToStr: ["nimBoolToStr", "nimBoolToStr"],
  423. mIntToStr: ["cstrToNimstr", "cstrToNimstr"],
  424. mInt64ToStr: ["cstrToNimstr", "cstrToNimstr"],
  425. mFloatToStr: ["cstrToNimstr", "cstrToNimstr"],
  426. mCStrToStr: ["cstrToNimstr", "cstrToNimstr"],
  427. mStrToStr: ["", ""]]
  428. proc needsTemp(p: PProc; n: PNode): bool =
  429. # check if n contains a call to determine
  430. # if a temp should be made to prevent multiple evals
  431. result = false
  432. if n.kind in nkCallKinds + {nkTupleConstr, nkObjConstr, nkBracket, nkCurly}:
  433. return true
  434. for c in n:
  435. if needsTemp(p, c):
  436. return true
  437. proc maybeMakeTemp(p: PProc, n: PNode; x: TCompRes): tuple[a, tmp: Rope] =
  438. var
  439. a = x.rdLoc
  440. b = a
  441. if needsTemp(p, n):
  442. # if we have tmp just use it
  443. if x.tmpLoc != "" and (mapType(n.typ) == etyBaseIndex or n.kind in {nkHiddenDeref, nkDerefExpr}):
  444. b = "$1[0][$1[1]]" % [x.tmpLoc]
  445. (a: a, tmp: b)
  446. else:
  447. let tmp = p.getTemp
  448. b = tmp
  449. a = "($1 = $2, $1)" % [tmp, a]
  450. (a: a, tmp: b)
  451. else:
  452. (a: a, tmp: b)
  453. proc maybeMakeTempAssignable(p: PProc, n: PNode; x: TCompRes): tuple[a, tmp: Rope] =
  454. var
  455. a = x.rdLoc
  456. b = a
  457. if needsTemp(p, n):
  458. # if we have tmp just use it
  459. if x.tmpLoc != "" and (mapType(n.typ) == etyBaseIndex or n.kind in {nkHiddenDeref, nkDerefExpr}):
  460. b = "$1[0][$1[1]]" % [x.tmpLoc]
  461. result = (a: a, tmp: b)
  462. elif x.tmpLoc != "" and n.kind == nkBracketExpr:
  463. # genArrayAddr
  464. var
  465. address, index: TCompRes = default(TCompRes)
  466. first: Int128 = Zero
  467. gen(p, n[0], address)
  468. gen(p, n[1], index)
  469. let (m1, tmp1) = maybeMakeTemp(p, n[0], address)
  470. let typ = skipTypes(n[0].typ, abstractPtrs)
  471. if typ.kind == tyArray:
  472. first = firstOrd(p.config, typ[0])
  473. if optBoundsCheck in p.options:
  474. useMagic(p, "chckIndx")
  475. if first == 0: # save a couple chars
  476. index.res = "chckIndx($1, 0, ($2).length - 1)" % [index.res, tmp1]
  477. else:
  478. index.res = "chckIndx($1, $2, ($3).length + ($2) - 1) - ($2)" % [
  479. index.res, rope(first), tmp1]
  480. elif first != 0:
  481. index.res = "($1) - ($2)" % [index.res, rope(first)]
  482. else:
  483. discard # index.res = index.res
  484. let (n1, tmp2) = maybeMakeTemp(p, n[1], index)
  485. result = (a: "$1[$2]" % [m1, n1], tmp: "$1[$2]" % [tmp1, tmp2])
  486. # could also put here: nkDotExpr -> genFieldAccess, nkCheckedFieldExpr -> genCheckedFieldOp
  487. # but the uses of maybeMakeTempAssignable don't need them
  488. else:
  489. result = (a: a, tmp: b)
  490. else:
  491. result = (a: a, tmp: b)
  492. template binaryExpr(p: PProc, n: PNode, r: var TCompRes, magic, frmt: string,
  493. reassign = false) =
  494. # $1 and $2 in the `frmt` string bind to lhs and rhs of the expr,
  495. # if $3 or $4 are present they will be substituted with temps for
  496. # lhs and rhs respectively
  497. var x, y: TCompRes = default(TCompRes)
  498. useMagic(p, magic)
  499. gen(p, n[1], x)
  500. gen(p, n[2], y)
  501. var
  502. a, tmp = x.rdLoc
  503. b, tmp2 = y.rdLoc
  504. when reassign:
  505. (a, tmp) = maybeMakeTempAssignable(p, n[1], x)
  506. else:
  507. when "$3" in frmt: (a, tmp) = maybeMakeTemp(p, n[1], x)
  508. when "$4" in frmt: (b, tmp2) = maybeMakeTemp(p, n[2], y)
  509. r.res = frmt % [a, b, tmp, tmp2]
  510. r.kind = resExpr
  511. proc unsignedTrimmer(size: BiggestInt): string =
  512. case size
  513. of 1: "& 0xff"
  514. of 2: "& 0xffff"
  515. of 4: ">>> 0"
  516. else: ""
  517. proc signedTrimmer(size: BiggestInt): string =
  518. # sign extension is done by shifting to the left and then back to the right
  519. "<< $1 >> $1" % [$(32 - size * 8)]
  520. proc binaryUintExpr(p: PProc, n: PNode, r: var TCompRes, op: string,
  521. reassign: static[bool] = false) =
  522. var x, y: TCompRes = default(TCompRes)
  523. gen(p, n[1], x)
  524. gen(p, n[2], y)
  525. let size = n[1].typ.skipTypes(abstractRange).size
  526. when reassign:
  527. let (a, tmp) = maybeMakeTempAssignable(p, n[1], x)
  528. if size == 8 and optJsBigInt64 in p.config.globalOptions:
  529. r.res = "$1 = BigInt.asUintN(64, ($4 $2 $3))" % [a, rope op, y.rdLoc, tmp]
  530. else:
  531. let trimmer = unsignedTrimmer(size)
  532. r.res = "$1 = (($5 $2 $3) $4)" % [a, rope op, y.rdLoc, trimmer, tmp]
  533. else:
  534. if size == 8 and optJsBigInt64 in p.config.globalOptions:
  535. r.res = "BigInt.asUintN(64, ($1 $2 $3))" % [x.rdLoc, rope op, y.rdLoc]
  536. else:
  537. let trimmer = unsignedTrimmer(size)
  538. r.res = "(($1 $2 $3) $4)" % [x.rdLoc, rope op, y.rdLoc, trimmer]
  539. r.kind = resExpr
  540. template ternaryExpr(p: PProc, n: PNode, r: var TCompRes, magic, frmt: string) =
  541. var x, y, z: TCompRes
  542. useMagic(p, magic)
  543. gen(p, n[1], x)
  544. gen(p, n[2], y)
  545. gen(p, n[3], z)
  546. r.res = frmt % [x.rdLoc, y.rdLoc, z.rdLoc]
  547. r.kind = resExpr
  548. template unaryExpr(p: PProc, n: PNode, r: var TCompRes, magic, frmt: string) =
  549. # $1 binds to n[1], if $2 is present it will be substituted to a tmp of $1
  550. useMagic(p, magic)
  551. gen(p, n[1], r)
  552. var a, tmp = r.rdLoc
  553. if "$2" in frmt: (a, tmp) = maybeMakeTemp(p, n[1], r)
  554. r.res = frmt % [a, tmp]
  555. r.kind = resExpr
  556. proc arithAux(p: PProc, n: PNode, r: var TCompRes, op: TMagic) =
  557. var
  558. x, y: TCompRes = default(TCompRes)
  559. xLoc, yLoc: Rope = ""
  560. let i = ord(optOverflowCheck notin p.options)
  561. useMagic(p, jsMagics[op][i])
  562. if n.len > 2:
  563. gen(p, n[1], x)
  564. gen(p, n[2], y)
  565. xLoc = x.rdLoc
  566. yLoc = y.rdLoc
  567. else:
  568. gen(p, n[1], r)
  569. xLoc = r.rdLoc
  570. template applyFormat(frmt) =
  571. r.res = frmt % [xLoc, yLoc]
  572. template applyFormat(frmtA, frmtB) =
  573. if i == 0: applyFormat(frmtA) else: applyFormat(frmtB)
  574. template bitwiseExpr(op: string) =
  575. let typ = n[1].typ.skipTypes(abstractVarRange)
  576. if typ.kind in {tyUInt, tyUInt32}:
  577. r.res = "(($1 $2 $3) >>> 0)" % [xLoc, op, yLoc]
  578. else:
  579. r.res = "($1 $2 $3)" % [xLoc, op, yLoc]
  580. case op
  581. of mAddI:
  582. if i == 0:
  583. if n[1].typ.size == 8 and optJsBigInt64 in p.config.globalOptions:
  584. useMagic(p, "addInt64")
  585. applyFormat("addInt64($1, $2)")
  586. else:
  587. applyFormat("addInt($1, $2)")
  588. else:
  589. applyFormat("($1 + $2)")
  590. of mSubI:
  591. if i == 0:
  592. if n[1].typ.size == 8 and optJsBigInt64 in p.config.globalOptions:
  593. useMagic(p, "subInt64")
  594. applyFormat("subInt64($1, $2)")
  595. else:
  596. applyFormat("subInt($1, $2)")
  597. else:
  598. applyFormat("($1 - $2)")
  599. of mMulI:
  600. if i == 0:
  601. if n[1].typ.size == 8 and optJsBigInt64 in p.config.globalOptions:
  602. useMagic(p, "mulInt64")
  603. applyFormat("mulInt64($1, $2)")
  604. else:
  605. applyFormat("mulInt($1, $2)")
  606. else:
  607. applyFormat("($1 * $2)")
  608. of mDivI:
  609. if n[1].typ.size == 8 and optJsBigInt64 in p.config.globalOptions:
  610. useMagic(p, "divInt64")
  611. applyFormat("divInt64($1, $2)", "$1 / $2")
  612. else:
  613. applyFormat("divInt($1, $2)", "Math.trunc($1 / $2)")
  614. of mModI:
  615. if n[1].typ.size == 8 and optJsBigInt64 in p.config.globalOptions:
  616. useMagic(p, "modInt64")
  617. applyFormat("modInt64($1, $2)", "$1 % $2")
  618. else:
  619. applyFormat("modInt($1, $2)", "Math.trunc($1 % $2)")
  620. of mSucc:
  621. let typ = n[1].typ.skipTypes(abstractVarRange)
  622. case typ.kind
  623. of tyUInt..tyUInt32:
  624. binaryUintExpr(p, n, r, "+")
  625. of tyUInt64:
  626. if optJsBigInt64 in p.config.globalOptions:
  627. applyFormat("BigInt.asUintN(64, $1 + BigInt($2))")
  628. else: binaryUintExpr(p, n, r, "+")
  629. elif typ.kind == tyInt64 and optJsBigInt64 in p.config.globalOptions:
  630. if optOverflowCheck notin p.options:
  631. applyFormat("BigInt.asIntN(64, $1 + BigInt($2))")
  632. else: binaryExpr(p, n, r, "addInt64", "addInt64($1, BigInt($2))")
  633. else:
  634. if optOverflowCheck notin p.options: applyFormat("$1 + $2")
  635. else: binaryExpr(p, n, r, "addInt", "addInt($1, $2)")
  636. of mPred:
  637. let typ = n[1].typ.skipTypes(abstractVarRange)
  638. case typ.kind
  639. of tyUInt..tyUInt32:
  640. binaryUintExpr(p, n, r, "-")
  641. of tyUInt64:
  642. if optJsBigInt64 in p.config.globalOptions:
  643. applyFormat("BigInt.asUintN(64, $1 - BigInt($2))")
  644. else: binaryUintExpr(p, n, r, "-")
  645. elif typ.kind == tyInt64 and optJsBigInt64 in p.config.globalOptions:
  646. if optOverflowCheck notin p.options:
  647. applyFormat("BigInt.asIntN(64, $1 - BigInt($2))")
  648. else: binaryExpr(p, n, r, "subInt64", "subInt64($1, BigInt($2))")
  649. else:
  650. if optOverflowCheck notin p.options: applyFormat("$1 - $2")
  651. else: binaryExpr(p, n, r, "subInt", "subInt($1, $2)")
  652. of mAddF64: applyFormat("($1 + $2)", "($1 + $2)")
  653. of mSubF64: applyFormat("($1 - $2)", "($1 - $2)")
  654. of mMulF64: applyFormat("($1 * $2)", "($1 * $2)")
  655. of mDivF64: applyFormat("($1 / $2)", "($1 / $2)")
  656. of mShrI:
  657. let typ = n[1].typ.skipTypes(abstractVarRange)
  658. if typ.kind == tyInt64 and optJsBigInt64 in p.config.globalOptions:
  659. applyFormat("BigInt.asIntN(64, BigInt.asUintN(64, $1) >> BigInt($2))")
  660. elif typ.kind == tyUInt64 and optJsBigInt64 in p.config.globalOptions:
  661. applyFormat("($1 >> BigInt($2))")
  662. else:
  663. if typ.kind in {tyInt..tyInt32}:
  664. let trimmerU = unsignedTrimmer(typ.size)
  665. let trimmerS = signedTrimmer(typ.size)
  666. r.res = "((($1 $2) >>> $3) $4)" % [xLoc, trimmerU, yLoc, trimmerS]
  667. else:
  668. applyFormat("($1 >>> $2)")
  669. of mShlI:
  670. let typ = n[1].typ.skipTypes(abstractVarRange)
  671. if typ.size == 8:
  672. if typ.kind == tyInt64 and optJsBigInt64 in p.config.globalOptions:
  673. applyFormat("BigInt.asIntN(64, $1 << BigInt($2))")
  674. elif typ.kind == tyUInt64 and optJsBigInt64 in p.config.globalOptions:
  675. applyFormat("BigInt.asUintN(64, $1 << BigInt($2))")
  676. else:
  677. applyFormat("($1 * Math.pow(2, $2))")
  678. else:
  679. if typ.kind in {tyUInt..tyUInt32}:
  680. let trimmer = unsignedTrimmer(typ.size)
  681. r.res = "(($1 << $2) $3)" % [xLoc, yLoc, trimmer]
  682. else:
  683. let trimmer = signedTrimmer(typ.size)
  684. r.res = "(($1 << $2) $3)" % [xLoc, yLoc, trimmer]
  685. of mAshrI:
  686. let typ = n[1].typ.skipTypes(abstractVarRange)
  687. if typ.size == 8:
  688. if optJsBigInt64 in p.config.globalOptions:
  689. applyFormat("($1 >> BigInt($2))")
  690. else:
  691. applyFormat("Math.floor($1 / Math.pow(2, $2))")
  692. else:
  693. if typ.kind in {tyUInt..tyUInt32}:
  694. applyFormat("($1 >>> $2)")
  695. else:
  696. applyFormat("($1 >> $2)")
  697. of mBitandI: bitwiseExpr("&")
  698. of mBitorI: bitwiseExpr("|")
  699. of mBitxorI: bitwiseExpr("^")
  700. of mMinI: applyFormat("nimMin($1, $2)", "nimMin($1, $2)")
  701. of mMaxI: applyFormat("nimMax($1, $2)", "nimMax($1, $2)")
  702. of mAddU: applyFormat("", "")
  703. of mSubU: applyFormat("", "")
  704. of mMulU: applyFormat("", "")
  705. of mDivU: applyFormat("", "")
  706. of mModU: applyFormat("($1 % $2)", "($1 % $2)")
  707. of mEqI: applyFormat("($1 == $2)", "($1 == $2)")
  708. of mLeI: applyFormat("($1 <= $2)", "($1 <= $2)")
  709. of mLtI: applyFormat("($1 < $2)", "($1 < $2)")
  710. of mEqF64: applyFormat("($1 == $2)", "($1 == $2)")
  711. of mLeF64: applyFormat("($1 <= $2)", "($1 <= $2)")
  712. of mLtF64: applyFormat("($1 < $2)", "($1 < $2)")
  713. of mLeU: applyFormat("($1 <= $2)", "($1 <= $2)")
  714. of mLtU: applyFormat("($1 < $2)", "($1 < $2)")
  715. of mEqEnum: applyFormat("($1 == $2)", "($1 == $2)")
  716. of mLeEnum: applyFormat("($1 <= $2)", "($1 <= $2)")
  717. of mLtEnum: applyFormat("($1 < $2)", "($1 < $2)")
  718. of mEqCh: applyFormat("($1 == $2)", "($1 == $2)")
  719. of mLeCh: applyFormat("($1 <= $2)", "($1 <= $2)")
  720. of mLtCh: applyFormat("($1 < $2)", "($1 < $2)")
  721. of mEqB: applyFormat("($1 == $2)", "($1 == $2)")
  722. of mLeB: applyFormat("($1 <= $2)", "($1 <= $2)")
  723. of mLtB: applyFormat("($1 < $2)", "($1 < $2)")
  724. of mEqRef: applyFormat("($1 == $2)", "($1 == $2)")
  725. of mLePtr: applyFormat("($1 <= $2)", "($1 <= $2)")
  726. of mLtPtr: applyFormat("($1 < $2)", "($1 < $2)")
  727. of mXor: applyFormat("($1 != $2)", "($1 != $2)")
  728. of mEqCString: applyFormat("($1 == $2)", "($1 == $2)")
  729. of mEqProc: applyFormat("($1 == $2)", "($1 == $2)")
  730. of mUnaryMinusI: applyFormat("negInt($1)", "-($1)")
  731. of mUnaryMinusI64: applyFormat("negInt64($1)", "-($1)")
  732. of mAbsI: applyFormat("absInt($1)", "Math.abs($1)")
  733. of mNot: applyFormat("!($1)", "!($1)")
  734. of mUnaryPlusI: applyFormat("+($1)", "+($1)")
  735. of mBitnotI:
  736. let typ = n[1].typ.skipTypes(abstractVarRange)
  737. if typ.kind in {tyUInt..tyUInt64}:
  738. if typ.size == 8 and optJsBigInt64 in p.config.globalOptions:
  739. applyFormat("BigInt.asUintN(64, ~($1))")
  740. else:
  741. let trimmer = unsignedTrimmer(typ.size)
  742. r.res = "(~($1) $2)" % [xLoc, trimmer]
  743. else:
  744. applyFormat("~($1)")
  745. of mUnaryPlusF64: applyFormat("+($1)", "+($1)")
  746. of mUnaryMinusF64: applyFormat("-($1)", "-($1)")
  747. of mCharToStr: applyFormat("nimCharToStr($1)", "nimCharToStr($1)")
  748. of mBoolToStr: applyFormat("nimBoolToStr($1)", "nimBoolToStr($1)")
  749. of mIntToStr: applyFormat("cstrToNimstr(($1) + \"\")", "cstrToNimstr(($1) + \"\")")
  750. of mInt64ToStr: applyFormat("cstrToNimstr(($1) + \"\")", "cstrToNimstr(($1) + \"\")")
  751. of mCStrToStr: applyFormat("cstrToNimstr($1)", "cstrToNimstr($1)")
  752. of mStrToStr, mUnown, mIsolate, mFinished: applyFormat("$1", "$1")
  753. else:
  754. assert false, $op
  755. proc arith(p: PProc, n: PNode, r: var TCompRes, op: TMagic) =
  756. case op
  757. of mAddU: binaryUintExpr(p, n, r, "+")
  758. of mSubU: binaryUintExpr(p, n, r, "-")
  759. of mMulU: binaryUintExpr(p, n, r, "*")
  760. of mDivU:
  761. binaryUintExpr(p, n, r, "/")
  762. if optJsBigInt64 notin p.config.globalOptions and
  763. n[1].typ.skipTypes(abstractRange).size == 8:
  764. # bigint / already truncates
  765. r.res = "Math.trunc($1)" % [r.res]
  766. of mDivI:
  767. arithAux(p, n, r, op)
  768. of mModI:
  769. arithAux(p, n, r, op)
  770. of mCharToStr, mBoolToStr, mIntToStr, mInt64ToStr, mCStrToStr, mStrToStr, mEnumToStr:
  771. arithAux(p, n, r, op)
  772. of mEqRef:
  773. if mapType(n[1].typ) != etyBaseIndex:
  774. arithAux(p, n, r, op)
  775. else:
  776. var x, y: TCompRes = default(TCompRes)
  777. gen(p, n[1], x)
  778. gen(p, n[2], y)
  779. r.res = "($# == $# && $# == $#)" % [x.address, y.address, x.res, y.res]
  780. else:
  781. arithAux(p, n, r, op)
  782. r.kind = resExpr
  783. proc hasFrameInfo(p: PProc): bool =
  784. ({optLineTrace, optStackTrace} * p.options == {optLineTrace, optStackTrace}) and
  785. ((p.prc == nil) or not (sfPure in p.prc.flags))
  786. proc lineDir(config: ConfigRef, info: TLineInfo, line: int): Rope =
  787. "/* line $2:$3 \"$1\" */$n" % [
  788. rope(toFullPath(config, info)), rope(line), rope(info.toColumn)
  789. ]
  790. proc genLineDir(p: PProc, n: PNode) =
  791. let line = toLinenumber(n.info)
  792. if line < 0:
  793. return
  794. if optEmbedOrigSrc in p.config.globalOptions:
  795. lineF(p, "//$1$n", [sourceLine(p.config, n.info)])
  796. if optLineDir in p.options or optLineDir in p.config.options:
  797. lineF(p, "$1", [lineDir(p.config, n.info, line)])
  798. if hasFrameInfo(p):
  799. lineF(p, "F.line = $1;$n", [rope(line)])
  800. let currentFileName = toFilename(p.config, n.info)
  801. if p.previousFileName != currentFileName:
  802. lineF(p, "F.filename = $1;$n", [makeJSString(currentFileName)])
  803. p.previousFileName = currentFileName
  804. proc genWhileStmt(p: PProc, n: PNode) =
  805. var cond: TCompRes = default(TCompRes)
  806. internalAssert p.config, isEmptyType(n.typ)
  807. genLineDir(p, n)
  808. inc(p.unique)
  809. setLen(p.blocks, p.blocks.len + 1)
  810. p.blocks[^1].id = -p.unique
  811. p.blocks[^1].isLoop = true
  812. let labl = p.unique.rope
  813. lineF(p, "Label$1: while (true) {$n", [labl])
  814. p.nested: gen(p, n[0], cond)
  815. lineF(p, "if (!$1) break Label$2;$n",
  816. [cond.res, labl])
  817. p.nested: genStmt(p, n[1])
  818. lineF(p, "}$n", [labl])
  819. setLen(p.blocks, p.blocks.len - 1)
  820. proc moveInto(p: PProc, src: var TCompRes, dest: TCompRes) =
  821. if src.kind != resNone:
  822. if dest.kind != resNone:
  823. lineF(p, "$1 = $2;$n", [dest.rdLoc, src.rdLoc])
  824. else:
  825. lineF(p, "$1;$n", [src.rdLoc])
  826. src.kind = resNone
  827. src.res = ""
  828. proc genTry(p: PProc, n: PNode, r: var TCompRes) =
  829. # code to generate:
  830. #
  831. # ++excHandler;
  832. # var tmpFramePtr = framePtr;
  833. # try {
  834. # stmts;
  835. # --excHandler;
  836. # } catch (EXCEPTION) {
  837. # var prevJSError = lastJSError; lastJSError = EXCEPTION;
  838. # framePtr = tmpFramePtr;
  839. # --excHandler;
  840. # if (e.typ && e.typ == NTI433 || e.typ == NTI2321) {
  841. # stmts;
  842. # } else if (e.typ && e.typ == NTI32342) {
  843. # stmts;
  844. # } else {
  845. # stmts;
  846. # }
  847. # lastJSError = prevJSError;
  848. # } finally {
  849. # framePtr = tmpFramePtr;
  850. # stmts;
  851. # }
  852. genLineDir(p, n)
  853. if not isEmptyType(n.typ):
  854. r.kind = resVal
  855. r.res = getTemp(p)
  856. inc(p.unique)
  857. var i = 1
  858. var catchBranchesExist = n.len > 1 and n[i].kind == nkExceptBranch
  859. if catchBranchesExist:
  860. p.body.add("++excHandler;\L")
  861. var tmpFramePtr = rope"F"
  862. lineF(p, "try {$n", [])
  863. var a: TCompRes
  864. gen(p, n[0], a)
  865. moveInto(p, a, r)
  866. var generalCatchBranchExists = false
  867. if catchBranchesExist:
  868. p.body.addf("--excHandler;$n} catch (EXCEPTION) {$n var prevJSError = lastJSError;$n" &
  869. " lastJSError = EXCEPTION;$n --excHandler;$n", [])
  870. if hasFrameInfo(p):
  871. line(p, "framePtr = $1;$n" % [tmpFramePtr])
  872. while i < n.len and n[i].kind == nkExceptBranch:
  873. if n[i].len == 1:
  874. # general except section:
  875. generalCatchBranchExists = true
  876. if i > 1: lineF(p, "else {$n", [])
  877. gen(p, n[i][0], a)
  878. moveInto(p, a, r)
  879. if i > 1: lineF(p, "}$n", [])
  880. else:
  881. var orExpr: Rope = ""
  882. var excAlias: PNode = nil
  883. useMagic(p, "isObj")
  884. for j in 0..<n[i].len - 1:
  885. var throwObj: PNode
  886. let it = n[i][j]
  887. if it.isInfixAs():
  888. throwObj = it[1]
  889. excAlias = it[2]
  890. # If this is a ``except exc as sym`` branch there must be no following
  891. # nodes
  892. doAssert orExpr == ""
  893. elif it.kind == nkType:
  894. throwObj = it
  895. else:
  896. throwObj = nil
  897. internalError(p.config, n.info, "genTryStmt")
  898. if orExpr != "": orExpr.add("||")
  899. # Generate the correct type checking code depending on whether this is a
  900. # NIM-native or a JS-native exception
  901. # if isJsObject(throwObj.typ):
  902. if isImportedException(throwObj.typ, p.config):
  903. orExpr.addf("lastJSError instanceof $1",
  904. [throwObj.typ.sym.loc.r])
  905. else:
  906. orExpr.addf("isObj(lastJSError.m_type, $1)",
  907. [genTypeInfo(p, throwObj.typ)])
  908. if i > 1: line(p, "else ")
  909. lineF(p, "if (lastJSError && ($1)) {$n", [orExpr])
  910. # If some branch requires a local alias introduce it here. This is needed
  911. # since JS cannot do ``catch x as y``.
  912. if excAlias != nil:
  913. excAlias.sym.loc.r = mangleName(p.module, excAlias.sym)
  914. lineF(p, "var $1 = lastJSError;$n", excAlias.sym.loc.r)
  915. gen(p, n[i][^1], a)
  916. moveInto(p, a, r)
  917. lineF(p, "}$n", [])
  918. inc(i)
  919. if catchBranchesExist:
  920. if not generalCatchBranchExists:
  921. useMagic(p, "reraiseException")
  922. line(p, "else {\L")
  923. line(p, "\treraiseException();\L")
  924. line(p, "}\L")
  925. lineF(p, "lastJSError = prevJSError;$n")
  926. line(p, "} finally {\L")
  927. if hasFrameInfo(p):
  928. line(p, "framePtr = $1;$n" % [tmpFramePtr])
  929. if i < n.len and n[i].kind == nkFinally:
  930. genStmt(p, n[i][0])
  931. line(p, "}\L")
  932. proc genRaiseStmt(p: PProc, n: PNode) =
  933. if n[0].kind != nkEmpty:
  934. var a: TCompRes = default(TCompRes)
  935. gen(p, n[0], a)
  936. let typ = skipTypes(n[0].typ, abstractPtrs)
  937. genLineDir(p, n)
  938. useMagic(p, "raiseException")
  939. lineF(p, "raiseException($1, $2);$n",
  940. [a.rdLoc, makeJSString(typ.sym.name.s)])
  941. else:
  942. genLineDir(p, n)
  943. useMagic(p, "reraiseException")
  944. line(p, "reraiseException();\L")
  945. proc genCaseJS(p: PProc, n: PNode, r: var TCompRes) =
  946. var
  947. a, b, cond, stmt: TCompRes = default(TCompRes)
  948. genLineDir(p, n)
  949. gen(p, n[0], cond)
  950. let typeKind = skipTypes(n[0].typ, abstractVar).kind
  951. var transferRange = false
  952. let anyString = typeKind in {tyString, tyCstring}
  953. case typeKind
  954. of tyString:
  955. useMagic(p, "toJSStr")
  956. lineF(p, "switch (toJSStr($1)) {$n", [cond.rdLoc])
  957. of tyFloat..tyFloat128, tyInt..tyInt64, tyUInt..tyUInt64:
  958. transferRange = true
  959. else:
  960. lineF(p, "switch ($1) {$n", [cond.rdLoc])
  961. if not isEmptyType(n.typ):
  962. r.kind = resVal
  963. r.res = getTemp(p)
  964. for i in 1..<n.len:
  965. let it = n[i]
  966. let itLen = it.len
  967. case it.kind
  968. of nkOfBranch:
  969. if transferRange:
  970. if i == 1:
  971. lineF(p, "if (", [])
  972. else:
  973. lineF(p, "else if (", [])
  974. for j in 0..<itLen - 1:
  975. let e = it[j]
  976. if e.kind == nkRange:
  977. if transferRange:
  978. gen(p, e[0], a)
  979. gen(p, e[1], b)
  980. if j != itLen - 2:
  981. lineF(p, "$1 >= $2 && $1 <= $3 || $n", [cond.rdLoc, a.rdLoc, b.rdLoc])
  982. else:
  983. lineF(p, "$1 >= $2 && $1 <= $3", [cond.rdLoc, a.rdLoc, b.rdLoc])
  984. else:
  985. var v = copyNode(e[0])
  986. while v.intVal <= e[1].intVal:
  987. gen(p, v, cond)
  988. lineF(p, "case $1:$n", [cond.rdLoc])
  989. inc(v.intVal)
  990. else:
  991. if anyString:
  992. case e.kind
  993. of nkStrLit..nkTripleStrLit: lineF(p, "case $1:$n",
  994. [makeJSString(e.strVal, false)])
  995. of nkNilLit: lineF(p, "case null:$n", [])
  996. else: internalError(p.config, e.info, "jsgen.genCaseStmt: 2")
  997. else:
  998. if transferRange:
  999. gen(p, e, a)
  1000. if j != itLen - 2:
  1001. lineF(p, "$1 == $2 || $n", [cond.rdLoc, a.rdLoc])
  1002. else:
  1003. lineF(p, "$1 == $2", [cond.rdLoc, a.rdLoc])
  1004. else:
  1005. gen(p, e, a)
  1006. lineF(p, "case $1:$n", [a.rdLoc])
  1007. if transferRange:
  1008. lineF(p, "){", [])
  1009. p.nested:
  1010. gen(p, lastSon(it), stmt)
  1011. moveInto(p, stmt, r)
  1012. if transferRange:
  1013. lineF(p, "}$n", [])
  1014. else:
  1015. lineF(p, "break;$n", [])
  1016. of nkElse:
  1017. if transferRange:
  1018. lineF(p, "else{$n", [])
  1019. else:
  1020. lineF(p, "default: $n", [])
  1021. p.nested:
  1022. gen(p, it[0], stmt)
  1023. moveInto(p, stmt, r)
  1024. if transferRange:
  1025. lineF(p, "}$n", [])
  1026. else:
  1027. lineF(p, "break;$n", [])
  1028. else: internalError(p.config, it.info, "jsgen.genCaseStmt")
  1029. if not transferRange:
  1030. lineF(p, "}$n", [])
  1031. proc genBlock(p: PProc, n: PNode, r: var TCompRes) =
  1032. inc(p.unique)
  1033. let idx = p.blocks.len
  1034. if n[0].kind != nkEmpty:
  1035. # named block?
  1036. if (n[0].kind != nkSym): internalError(p.config, n.info, "genBlock")
  1037. var sym = n[0].sym
  1038. sym.loc.k = locOther
  1039. sym.position = idx+1
  1040. let labl = p.unique
  1041. lineF(p, "Label$1: {$n", [labl.rope])
  1042. setLen(p.blocks, idx + 1)
  1043. p.blocks[idx].id = - p.unique # negative because it isn't used yet
  1044. gen(p, n[1], r)
  1045. setLen(p.blocks, idx)
  1046. lineF(p, "};$n", [labl.rope])
  1047. proc genBreakStmt(p: PProc, n: PNode) =
  1048. var idx: int
  1049. genLineDir(p, n)
  1050. if n[0].kind != nkEmpty:
  1051. # named break?
  1052. assert(n[0].kind == nkSym)
  1053. let sym = n[0].sym
  1054. assert(sym.loc.k == locOther)
  1055. idx = sym.position-1
  1056. else:
  1057. # an unnamed 'break' can only break a loop after 'transf' pass:
  1058. idx = p.blocks.len - 1
  1059. while idx >= 0 and not p.blocks[idx].isLoop: dec idx
  1060. if idx < 0 or not p.blocks[idx].isLoop:
  1061. internalError(p.config, n.info, "no loop to break")
  1062. p.blocks[idx].id = abs(p.blocks[idx].id) # label is used
  1063. lineF(p, "break Label$1;$n", [rope(p.blocks[idx].id)])
  1064. proc genAsmOrEmitStmt(p: PProc, n: PNode) =
  1065. genLineDir(p, n)
  1066. p.body.add p.indentLine("")
  1067. for i in 0..<n.len:
  1068. let it = n[i]
  1069. case it.kind
  1070. of nkStrLit..nkTripleStrLit:
  1071. p.body.add(it.strVal)
  1072. of nkSym:
  1073. let v = it.sym
  1074. # for backwards compatibility we don't deref syms here :-(
  1075. if false:
  1076. discard
  1077. else:
  1078. var r = default(TCompRes)
  1079. gen(p, it, r)
  1080. if it.typ.kind == tyPointer:
  1081. # A fat pointer is disguised as an array
  1082. r.res = r.address
  1083. r.address = ""
  1084. r.typ = etyNone
  1085. elif r.typ == etyBaseIndex:
  1086. # Deference first
  1087. r.res = "$1[$2]" % [r.address, r.res]
  1088. r.address = ""
  1089. r.typ = etyNone
  1090. p.body.add(r.rdLoc)
  1091. else:
  1092. var r: TCompRes = default(TCompRes)
  1093. gen(p, it, r)
  1094. p.body.add(r.rdLoc)
  1095. p.body.add "\L"
  1096. proc genIf(p: PProc, n: PNode, r: var TCompRes) =
  1097. var cond, stmt: TCompRes = default(TCompRes)
  1098. var toClose = 0
  1099. if not isEmptyType(n.typ):
  1100. r.kind = resVal
  1101. r.res = getTemp(p)
  1102. for i in 0..<n.len:
  1103. let it = n[i]
  1104. if it.len != 1:
  1105. if i > 0:
  1106. lineF(p, "else {$n", [])
  1107. inc(toClose)
  1108. p.nested: gen(p, it[0], cond)
  1109. lineF(p, "if ($1) {$n", [cond.rdLoc])
  1110. gen(p, it[1], stmt)
  1111. else:
  1112. # else part:
  1113. lineF(p, "else {$n", [])
  1114. p.nested: gen(p, it[0], stmt)
  1115. moveInto(p, stmt, r)
  1116. lineF(p, "}$n", [])
  1117. line(p, repeat('}', toClose) & "\L")
  1118. proc generateHeader(p: PProc, typ: PType): Rope =
  1119. result = ""
  1120. for i in 1..<typ.n.len:
  1121. assert(typ.n[i].kind == nkSym)
  1122. var param = typ.n[i].sym
  1123. if isCompileTimeOnly(param.typ): continue
  1124. if result != "": result.add(", ")
  1125. var name = mangleName(p.module, param)
  1126. result.add(name)
  1127. if mapType(param.typ) == etyBaseIndex:
  1128. result.add(", ")
  1129. result.add(name)
  1130. result.add("_Idx")
  1131. proc countJsParams(typ: PType): int =
  1132. result = 0
  1133. for i in 1..<typ.n.len:
  1134. assert(typ.n[i].kind == nkSym)
  1135. var param = typ.n[i].sym
  1136. if isCompileTimeOnly(param.typ): continue
  1137. if mapType(param.typ) == etyBaseIndex:
  1138. inc result, 2
  1139. else:
  1140. inc result
  1141. const
  1142. nodeKindsNeedNoCopy = {nkCharLit..nkInt64Lit, nkStrLit..nkTripleStrLit,
  1143. nkFloatLit..nkFloat64Lit, nkPar, nkStringToCString,
  1144. nkObjConstr, nkTupleConstr, nkBracket,
  1145. nkCStringToString, nkCall, nkPrefix, nkPostfix, nkInfix,
  1146. nkCommand, nkHiddenCallConv, nkCallStrLit}
  1147. proc needsNoCopy(p: PProc; y: PNode): bool =
  1148. return y.kind in nodeKindsNeedNoCopy or
  1149. ((mapType(y.typ) != etyBaseIndex or (y.kind == nkSym and y.sym.kind == skParam)) and
  1150. (skipTypes(y.typ, abstractInst).kind in
  1151. {tyRef, tyPtr, tyLent, tyVar, tyCstring, tyProc, tyOwned} + IntegralTypes))
  1152. proc genAsgnAux(p: PProc, x, y: PNode, noCopyNeeded: bool) =
  1153. var a, b: TCompRes = default(TCompRes)
  1154. var xtyp = mapType(p, x.typ)
  1155. # disable `[]=` for cstring
  1156. if x.kind == nkBracketExpr and x.len >= 2 and x[0].typ.skipTypes(abstractInst).kind == tyCstring:
  1157. localError(p.config, x.info, "cstring doesn't support `[]=` operator")
  1158. gen(p, x, a)
  1159. genLineDir(p, y)
  1160. gen(p, y, b)
  1161. # we don't care if it's an etyBaseIndex (global) of a string, it's
  1162. # still a string that needs to be copied properly:
  1163. if x.typ.skipTypes(abstractInst).kind in {tySequence, tyString}:
  1164. xtyp = etySeq
  1165. case xtyp
  1166. of etySeq:
  1167. if x.typ.kind in {tyVar, tyLent} or (needsNoCopy(p, y) and needsNoCopy(p, x)) or noCopyNeeded:
  1168. lineF(p, "$1 = $2;$n", [a.rdLoc, b.rdLoc])
  1169. else:
  1170. useMagic(p, "nimCopy")
  1171. lineF(p, "$1 = nimCopy(null, $2, $3);$n",
  1172. [a.rdLoc, b.res, genTypeInfo(p, y.typ)])
  1173. of etyObject:
  1174. if x.typ.kind in {tyVar, tyLent} or (needsNoCopy(p, y) and needsNoCopy(p, x)) or noCopyNeeded:
  1175. lineF(p, "$1 = $2;$n", [a.rdLoc, b.rdLoc])
  1176. else:
  1177. useMagic(p, "nimCopy")
  1178. # supports proc getF(): var T
  1179. if x.kind in {nkHiddenDeref, nkDerefExpr} and x[0].kind in nkCallKinds:
  1180. lineF(p, "nimCopy($1, $2, $3);$n",
  1181. [a.res, b.res, genTypeInfo(p, x.typ)])
  1182. else:
  1183. lineF(p, "$1 = nimCopy($1, $2, $3);$n",
  1184. [a.res, b.res, genTypeInfo(p, x.typ)])
  1185. of etyBaseIndex:
  1186. if a.typ != etyBaseIndex or b.typ != etyBaseIndex:
  1187. if y.kind == nkCall:
  1188. let tmp = p.getTemp(false)
  1189. lineF(p, "var $1 = $4; $2 = $1[0]; $3 = $1[1];$n", [tmp, a.address, a.res, b.rdLoc])
  1190. elif b.typ == etyBaseIndex:
  1191. lineF(p, "$# = [$#, $#];$n", [a.res, b.address, b.res])
  1192. elif b.typ == etyNone:
  1193. internalAssert p.config, b.address == ""
  1194. lineF(p, "$# = [$#, 0];$n", [a.address, b.res])
  1195. elif x.typ.kind == tyVar and y.typ.kind == tyPtr:
  1196. lineF(p, "$# = [$#, $#];$n", [a.res, b.address, b.res])
  1197. lineF(p, "$1 = $2;$n", [a.address, b.res])
  1198. lineF(p, "$1 = $2;$n", [a.rdLoc, b.rdLoc])
  1199. elif a.typ == etyBaseIndex:
  1200. # array indexing may not map to var type
  1201. if b.address != "":
  1202. lineF(p, "$1 = $2; $3 = $4;$n", [a.address, b.address, a.res, b.res])
  1203. else:
  1204. lineF(p, "$1 = $2;$n", [a.address, b.res])
  1205. else:
  1206. internalError(p.config, x.info, $("genAsgn", b.typ, a.typ))
  1207. elif b.address != "":
  1208. lineF(p, "$1 = $2; $3 = $4;$n", [a.address, b.address, a.res, b.res])
  1209. else:
  1210. lineF(p, "$1 = $2;$n", [a.address, b.res])
  1211. else:
  1212. lineF(p, "$1 = $2;$n", [a.rdLoc, b.rdLoc])
  1213. proc genAsgn(p: PProc, n: PNode) =
  1214. genAsgnAux(p, n[0], n[1], noCopyNeeded=false)
  1215. proc genFastAsgn(p: PProc, n: PNode) =
  1216. # 'shallowCopy' always produced 'noCopyNeeded = true' here but this is wrong
  1217. # for code like
  1218. # while j >= pos:
  1219. # dest[i].shallowCopy(dest[j])
  1220. # See bug #5933. So we try to be more compatible with the C backend semantics
  1221. # here for 'shallowCopy'. This is an educated guess and might require further
  1222. # changes later:
  1223. let noCopy = n[0].typ.skipTypes(abstractInst).kind in {tySequence, tyString}
  1224. genAsgnAux(p, n[0], n[1], noCopyNeeded=noCopy)
  1225. proc genSwap(p: PProc, n: PNode) =
  1226. var a, b: TCompRes = default(TCompRes)
  1227. gen(p, n[1], a)
  1228. gen(p, n[2], b)
  1229. var tmp = p.getTemp(false)
  1230. if mapType(p, skipTypes(n[1].typ, abstractVar)) == etyBaseIndex:
  1231. let tmp2 = p.getTemp(false)
  1232. if a.typ != etyBaseIndex or b.typ != etyBaseIndex:
  1233. internalError(p.config, n.info, "genSwap")
  1234. lineF(p, "var $1 = $2; $2 = $3; $3 = $1;$n",
  1235. [tmp, a.address, b.address])
  1236. tmp = tmp2
  1237. lineF(p, "var $1 = $2; $2 = $3; $3 = $1;",
  1238. [tmp, a.res, b.res])
  1239. proc getFieldPosition(p: PProc; f: PNode): int =
  1240. case f.kind
  1241. of nkIntLit..nkUInt64Lit: result = int(f.intVal)
  1242. of nkSym: result = f.sym.position
  1243. else:
  1244. result = 0
  1245. internalError(p.config, f.info, "genFieldPosition")
  1246. proc genFieldAddr(p: PProc, n: PNode, r: var TCompRes) =
  1247. var a: TCompRes = default(TCompRes)
  1248. r.typ = etyBaseIndex
  1249. let b = if n.kind == nkHiddenAddr: n[0] else: n
  1250. gen(p, b[0], a)
  1251. if skipTypes(b[0].typ, abstractVarRange).kind == tyTuple:
  1252. r.res = makeJSString("Field" & $getFieldPosition(p, b[1]))
  1253. else:
  1254. if b[1].kind != nkSym: internalError(p.config, b[1].info, "genFieldAddr")
  1255. var f = b[1].sym
  1256. if f.loc.r == "": f.loc.r = mangleName(p.module, f)
  1257. r.res = makeJSString($f.loc.r)
  1258. internalAssert p.config, a.typ != etyBaseIndex
  1259. r.address = a.res
  1260. r.kind = resExpr
  1261. proc genFieldAccess(p: PProc, n: PNode, r: var TCompRes) =
  1262. gen(p, n[0], r)
  1263. r.typ = mapType(n.typ)
  1264. let otyp = skipTypes(n[0].typ, abstractVarRange)
  1265. template mkTemp(i: int) =
  1266. if r.typ == etyBaseIndex:
  1267. if needsTemp(p, n[i]):
  1268. let tmp = p.getTemp
  1269. r.address = "($1 = $2, $1)[0]" % [tmp, r.res]
  1270. r.res = "$1[1]" % [tmp]
  1271. r.tmpLoc = tmp
  1272. else:
  1273. r.address = "$1[0]" % [r.res]
  1274. r.res = "$1[1]" % [r.res]
  1275. if otyp.kind == tyTuple:
  1276. r.res = ("$1.Field$2") %
  1277. [r.res, getFieldPosition(p, n[1]).rope]
  1278. mkTemp(0)
  1279. else:
  1280. if n[1].kind != nkSym: internalError(p.config, n[1].info, "genFieldAccess")
  1281. var f = n[1].sym
  1282. if f.loc.r == "": f.loc.r = mangleName(p.module, f)
  1283. r.res = "$1.$2" % [r.res, f.loc.r]
  1284. mkTemp(1)
  1285. r.kind = resExpr
  1286. proc genAddr(p: PProc, n: PNode, r: var TCompRes)
  1287. proc genCheckedFieldOp(p: PProc, n: PNode, addrTyp: PType, r: var TCompRes) =
  1288. internalAssert p.config, n.kind == nkCheckedFieldExpr
  1289. # nkDotExpr to access the requested field
  1290. let accessExpr = n[0]
  1291. # nkCall to check if the discriminant is valid
  1292. var checkExpr = n[1]
  1293. let negCheck = checkExpr[0].sym.magic == mNot
  1294. if negCheck:
  1295. checkExpr = checkExpr[^1]
  1296. # Field symbol
  1297. var field = accessExpr[1].sym
  1298. internalAssert p.config, field.kind == skField
  1299. if field.loc.r == "": field.loc.r = mangleName(p.module, field)
  1300. # Discriminant symbol
  1301. let disc = checkExpr[2].sym
  1302. internalAssert p.config, disc.kind == skField
  1303. if disc.loc.r == "": disc.loc.r = mangleName(p.module, disc)
  1304. var setx: TCompRes = default(TCompRes)
  1305. gen(p, checkExpr[1], setx)
  1306. var obj: TCompRes = default(TCompRes)
  1307. gen(p, accessExpr[0], obj)
  1308. # Avoid evaluating the LHS twice (one to read the discriminant and one to read
  1309. # the field)
  1310. let tmp = p.getTemp()
  1311. lineF(p, "var $1 = $2;$n", tmp, obj.res)
  1312. useMagic(p, "raiseFieldError2")
  1313. useMagic(p, "makeNimstrLit")
  1314. useMagic(p, "reprDiscriminant") # no need to offset by firstOrd unlike for cgen
  1315. let msg = genFieldDefect(p.config, field.name.s, disc)
  1316. lineF(p, "if ($1[$2.$3]$4undefined) { raiseFieldError2(makeNimstrLit($5), reprDiscriminant($2.$3, $6)); }$n",
  1317. setx.res, tmp, disc.loc.r, if negCheck: "!==" else: "===",
  1318. makeJSString(msg), genTypeInfo(p, disc.typ))
  1319. if addrTyp != nil and mapType(p, addrTyp) == etyBaseIndex:
  1320. r.typ = etyBaseIndex
  1321. r.res = makeJSString($field.loc.r)
  1322. r.address = tmp
  1323. else:
  1324. r.typ = etyNone
  1325. r.res = "$1.$2" % [tmp, field.loc.r]
  1326. r.kind = resExpr
  1327. proc genArrayAddr(p: PProc, n: PNode, r: var TCompRes) =
  1328. var
  1329. a, b: TCompRes = default(TCompRes)
  1330. first: Int128 = Zero
  1331. r.typ = etyBaseIndex
  1332. let m = if n.kind == nkHiddenAddr: n[0] else: n
  1333. gen(p, m[0], a)
  1334. gen(p, m[1], b)
  1335. #internalAssert p.config, a.typ != etyBaseIndex and b.typ != etyBaseIndex
  1336. let (x, tmp) = maybeMakeTemp(p, m[0], a)
  1337. r.address = x
  1338. var typ = skipTypes(m[0].typ, abstractPtrs)
  1339. if typ.kind == tyArray:
  1340. first = firstOrd(p.config, typ[0])
  1341. if optBoundsCheck in p.options:
  1342. useMagic(p, "chckIndx")
  1343. if first == 0: # save a couple chars
  1344. r.res = "chckIndx($1, 0, ($2).length - 1)" % [b.res, tmp]
  1345. else:
  1346. r.res = "chckIndx($1, $2, ($3).length + ($2) - 1) - ($2)" % [
  1347. b.res, rope(first), tmp]
  1348. elif first != 0:
  1349. r.res = "($1) - ($2)" % [b.res, rope(first)]
  1350. else:
  1351. r.res = b.res
  1352. r.kind = resExpr
  1353. proc genArrayAccess(p: PProc, n: PNode, r: var TCompRes) =
  1354. var ty = skipTypes(n[0].typ, abstractVarRange)
  1355. if ty.kind in {tyRef, tyPtr, tyLent, tyOwned}: ty = skipTypes(ty.lastSon, abstractVarRange)
  1356. case ty.kind
  1357. of tyArray, tyOpenArray, tySequence, tyString, tyCstring, tyVarargs:
  1358. genArrayAddr(p, n, r)
  1359. of tyTuple:
  1360. genFieldAddr(p, n, r)
  1361. else: internalError(p.config, n.info, "expr(nkBracketExpr, " & $ty.kind & ')')
  1362. r.typ = mapType(n.typ)
  1363. if r.res == "": internalError(p.config, n.info, "genArrayAccess")
  1364. if ty.kind == tyCstring:
  1365. r.res = "$1.charCodeAt($2)" % [r.address, r.res]
  1366. elif r.typ == etyBaseIndex:
  1367. if needsTemp(p, n[0]):
  1368. let tmp = p.getTemp
  1369. r.address = "($1 = $2, $1)[0]" % [tmp, r.rdLoc]
  1370. r.res = "$1[1]" % [tmp]
  1371. r.tmpLoc = tmp
  1372. else:
  1373. let x = r.rdLoc
  1374. r.address = "$1[0]" % [x]
  1375. r.res = "$1[1]" % [x]
  1376. else:
  1377. r.res = "$1[$2]" % [r.address, r.res]
  1378. r.kind = resExpr
  1379. template isIndirect(x: PSym): bool =
  1380. let v = x
  1381. ({sfAddrTaken, sfGlobal} * v.flags != {} and
  1382. #(mapType(v.typ) != etyObject) and
  1383. {sfImportc, sfExportc} * v.flags == {} and
  1384. v.kind notin {skProc, skFunc, skConverter, skMethod, skIterator,
  1385. skConst, skTemp, skLet})
  1386. proc genSymAddr(p: PProc, n: PNode, typ: PType, r: var TCompRes) =
  1387. let s = n.sym
  1388. if s.loc.r == "": internalError(p.config, n.info, "genAddr: 3")
  1389. case s.kind
  1390. of skParam:
  1391. r.res = s.loc.r
  1392. r.address = ""
  1393. r.typ = etyNone
  1394. of skVar, skLet, skResult:
  1395. r.kind = resExpr
  1396. let jsType = mapType(p):
  1397. if typ.isNil:
  1398. n.typ
  1399. else:
  1400. typ
  1401. if jsType == etyObject:
  1402. # make addr() a no-op:
  1403. r.typ = etyNone
  1404. if isIndirect(s):
  1405. r.res = s.loc.r & "[0]"
  1406. else:
  1407. r.res = s.loc.r
  1408. r.address = ""
  1409. elif {sfGlobal, sfAddrTaken} * s.flags != {} or jsType == etyBaseIndex:
  1410. # for ease of code generation, we do not distinguish between
  1411. # sfAddrTaken and sfGlobal.
  1412. r.typ = etyBaseIndex
  1413. r.address = s.loc.r
  1414. r.res = rope("0")
  1415. else:
  1416. # 'var openArray' for instance produces an 'addr' but this is harmless:
  1417. gen(p, n, r)
  1418. #internalError(p.config, n.info, "genAddr: 4 " & renderTree(n))
  1419. else: internalError(p.config, n.info, $("genAddr: 2", s.kind))
  1420. proc genAddr(p: PProc, n: PNode, r: var TCompRes) =
  1421. if n.kind == nkSym:
  1422. genSymAddr(p, n, nil, r)
  1423. else:
  1424. case n[0].kind
  1425. of nkSym:
  1426. genSymAddr(p, n[0], n.typ, r)
  1427. of nkCheckedFieldExpr:
  1428. genCheckedFieldOp(p, n[0], n.typ, r)
  1429. of nkDotExpr:
  1430. if mapType(p, n.typ) == etyBaseIndex:
  1431. genFieldAddr(p, n[0], r)
  1432. else:
  1433. genFieldAccess(p, n[0], r)
  1434. of nkBracketExpr:
  1435. var ty = skipTypes(n[0].typ, abstractVarRange)
  1436. if ty.kind in MappedToObject:
  1437. gen(p, n[0], r)
  1438. else:
  1439. let kindOfIndexedExpr = skipTypes(n[0][0].typ, abstractVarRange).kind
  1440. case kindOfIndexedExpr
  1441. of tyArray, tyOpenArray, tySequence, tyString, tyCstring, tyVarargs:
  1442. genArrayAddr(p, n[0], r)
  1443. of tyTuple:
  1444. genFieldAddr(p, n[0], r)
  1445. of tyGenericBody:
  1446. genAddr(p, n[^1], r)
  1447. else: internalError(p.config, n[0].info, "expr(nkBracketExpr, " & $kindOfIndexedExpr & ')')
  1448. of nkObjDownConv:
  1449. gen(p, n[0], r)
  1450. of nkHiddenDeref:
  1451. gen(p, n[0], r)
  1452. of nkDerefExpr:
  1453. var x = n[0]
  1454. if n.kind == nkHiddenAddr:
  1455. x = n[0][0]
  1456. if n.typ.skipTypes(abstractVar).kind != tyOpenArray:
  1457. x.typ = n.typ
  1458. gen(p, x, r)
  1459. of nkHiddenAddr:
  1460. gen(p, n[0], r)
  1461. of nkConv:
  1462. genAddr(p, n[0], r)
  1463. of nkStmtListExpr:
  1464. if n.len == 1: gen(p, n[0], r)
  1465. else: internalError(p.config, n[0].info, "genAddr for complex nkStmtListExpr")
  1466. of nkCallKinds:
  1467. if n[0].typ.kind == tyOpenArray:
  1468. # 'var openArray' for instance produces an 'addr' but this is harmless:
  1469. # namely toOpenArray(a, 1, 3)
  1470. gen(p, n[0], r)
  1471. else:
  1472. internalError(p.config, n[0].info, "genAddr: " & $n[0].kind)
  1473. else:
  1474. internalError(p.config, n[0].info, "genAddr: " & $n[0].kind)
  1475. proc attachProc(p: PProc; content: Rope; s: PSym) =
  1476. p.g.code.add(content)
  1477. proc attachProc(p: PProc; s: PSym) =
  1478. let newp = genProc(p, s)
  1479. attachProc(p, newp, s)
  1480. proc genProcForSymIfNeeded(p: PProc, s: PSym) =
  1481. if not p.g.generatedSyms.containsOrIncl(s.id):
  1482. let newp = genProc(p, s)
  1483. var owner = p
  1484. while owner != nil and owner.prc != s.owner:
  1485. owner = owner.up
  1486. if owner != nil: owner.locals.add(newp)
  1487. else: attachProc(p, newp, s)
  1488. proc genCopyForParamIfNeeded(p: PProc, n: PNode) =
  1489. let s = n.sym
  1490. if p.prc == s.owner or needsNoCopy(p, n):
  1491. return
  1492. var owner = p.up
  1493. while true:
  1494. if owner == nil:
  1495. internalError(p.config, n.info, "couldn't find the owner proc of the closed over param: " & s.name.s)
  1496. if owner.prc == s.owner:
  1497. if not owner.generatedParamCopies.containsOrIncl(s.id):
  1498. let copy = "$1 = nimCopy(null, $1, $2);$n" % [s.loc.r, genTypeInfo(p, s.typ)]
  1499. owner.locals.add(owner.indentLine(copy))
  1500. return
  1501. owner = owner.up
  1502. proc genVarInit(p: PProc, v: PSym, n: PNode)
  1503. proc genSym(p: PProc, n: PNode, r: var TCompRes) =
  1504. var s = n.sym
  1505. case s.kind
  1506. of skVar, skLet, skParam, skTemp, skResult, skForVar:
  1507. if s.loc.r == "":
  1508. internalError(p.config, n.info, "symbol has no generated name: " & s.name.s)
  1509. if sfCompileTime in s.flags:
  1510. genVarInit(p, s, if s.astdef != nil: s.astdef else: newNodeI(nkEmpty, s.info))
  1511. if s.kind == skParam:
  1512. genCopyForParamIfNeeded(p, n)
  1513. let k = mapType(p, s.typ)
  1514. if k == etyBaseIndex:
  1515. r.typ = etyBaseIndex
  1516. if {sfAddrTaken, sfGlobal} * s.flags != {}:
  1517. if isIndirect(s):
  1518. r.address = "$1[0][0]" % [s.loc.r]
  1519. r.res = "$1[0][1]" % [s.loc.r]
  1520. else:
  1521. r.address = "$1[0]" % [s.loc.r]
  1522. r.res = "$1[1]" % [s.loc.r]
  1523. else:
  1524. r.address = s.loc.r
  1525. r.res = s.loc.r & "_Idx"
  1526. elif isIndirect(s):
  1527. r.res = "$1[0]" % [s.loc.r]
  1528. else:
  1529. r.res = s.loc.r
  1530. of skConst:
  1531. genConstant(p, s)
  1532. if s.loc.r == "":
  1533. internalError(p.config, n.info, "symbol has no generated name: " & s.name.s)
  1534. r.res = s.loc.r
  1535. of skProc, skFunc, skConverter, skMethod:
  1536. if sfCompileTime in s.flags:
  1537. localError(p.config, n.info, "request to generate code for .compileTime proc: " &
  1538. s.name.s)
  1539. discard mangleName(p.module, s)
  1540. r.res = s.loc.r
  1541. if lfNoDecl in s.loc.flags or s.magic notin generatedMagics or
  1542. {sfImportc, sfInfixCall} * s.flags != {}:
  1543. discard
  1544. elif s.kind == skMethod and getBody(p.module.graph, s).kind == nkEmpty:
  1545. # we cannot produce code for the dispatcher yet:
  1546. discard
  1547. elif sfForward in s.flags:
  1548. p.g.forwarded.add(s)
  1549. else:
  1550. genProcForSymIfNeeded(p, s)
  1551. else:
  1552. if s.loc.r == "":
  1553. internalError(p.config, n.info, "symbol has no generated name: " & s.name.s)
  1554. if mapType(p, s.typ) == etyBaseIndex:
  1555. r.address = s.loc.r
  1556. r.res = s.loc.r & "_Idx"
  1557. else:
  1558. r.res = s.loc.r
  1559. r.kind = resVal
  1560. proc genDeref(p: PProc, n: PNode, r: var TCompRes) =
  1561. let it = n[0]
  1562. let t = mapType(p, it.typ)
  1563. if t == etyObject or it.typ.kind == tyLent:
  1564. gen(p, it, r)
  1565. else:
  1566. var a: TCompRes = default(TCompRes)
  1567. gen(p, it, a)
  1568. r.kind = a.kind
  1569. r.typ = mapType(p, n.typ)
  1570. if r.typ == etyBaseIndex:
  1571. let tmp = p.getTemp
  1572. r.address = "($1 = $2, $1)[0]" % [tmp, a.rdLoc]
  1573. r.res = "$1[1]" % [tmp]
  1574. r.tmpLoc = tmp
  1575. elif a.typ == etyBaseIndex:
  1576. if a.tmpLoc != "":
  1577. r.tmpLoc = a.tmpLoc
  1578. r.res = a.rdLoc
  1579. else:
  1580. internalError(p.config, n.info, "genDeref")
  1581. proc genArgNoParam(p: PProc, n: PNode, r: var TCompRes) =
  1582. var a: TCompRes = default(TCompRes)
  1583. gen(p, n, a)
  1584. if a.typ == etyBaseIndex:
  1585. r.res.add(a.address)
  1586. r.res.add(", ")
  1587. r.res.add(a.res)
  1588. else:
  1589. r.res.add(a.res)
  1590. proc genArg(p: PProc, n: PNode, param: PSym, r: var TCompRes; emitted: ptr int = nil) =
  1591. var a: TCompRes = default(TCompRes)
  1592. gen(p, n, a)
  1593. if skipTypes(param.typ, abstractVar).kind in {tyOpenArray, tyVarargs} and
  1594. a.typ == etyBaseIndex:
  1595. r.res.add("$1[$2]" % [a.address, a.res])
  1596. elif a.typ == etyBaseIndex:
  1597. r.res.add(a.address)
  1598. r.res.add(", ")
  1599. r.res.add(a.res)
  1600. if emitted != nil: inc emitted[]
  1601. elif n.typ.kind in {tyVar, tyPtr, tyRef, tyLent, tyOwned} and
  1602. n.kind in nkCallKinds and mapType(param.typ) == etyBaseIndex:
  1603. # this fixes bug #5608:
  1604. let tmp = getTemp(p)
  1605. r.res.add("($1 = $2, $1[0]), $1[1]" % [tmp, a.rdLoc])
  1606. if emitted != nil: inc emitted[]
  1607. else:
  1608. r.res.add(a.res)
  1609. proc genArgs(p: PProc, n: PNode, r: var TCompRes; start=1) =
  1610. r.res.add("(")
  1611. var hasArgs = false
  1612. var typ = skipTypes(n[0].typ, abstractInst)
  1613. assert(typ.kind == tyProc)
  1614. assert(typ.len == typ.n.len)
  1615. var emitted = start-1
  1616. for i in start..<n.len:
  1617. let it = n[i]
  1618. var paramType: PNode = nil
  1619. if i < typ.len:
  1620. assert(typ.n[i].kind == nkSym)
  1621. paramType = typ.n[i]
  1622. if paramType.typ.isCompileTimeOnly: continue
  1623. if hasArgs: r.res.add(", ")
  1624. if paramType.isNil:
  1625. genArgNoParam(p, it, r)
  1626. else:
  1627. genArg(p, it, paramType.sym, r, addr emitted)
  1628. inc emitted
  1629. hasArgs = true
  1630. r.res.add(")")
  1631. when false:
  1632. # XXX look into this:
  1633. let jsp = countJsParams(typ)
  1634. if emitted != jsp and tfVarargs notin typ.flags:
  1635. localError(p.config, n.info, "wrong number of parameters emitted; expected: " & $jsp &
  1636. " but got: " & $emitted)
  1637. r.kind = resExpr
  1638. proc genOtherArg(p: PProc; n: PNode; i: int; typ: PType;
  1639. generated: var int; r: var TCompRes) =
  1640. if i >= n.len:
  1641. globalError(p.config, n.info, "wrong importcpp pattern; expected parameter at position " & $i &
  1642. " but got only: " & $(n.len-1))
  1643. let it = n[i]
  1644. var paramType: PNode = nil
  1645. if i < typ.len:
  1646. assert(typ.n[i].kind == nkSym)
  1647. paramType = typ.n[i]
  1648. if paramType.typ.isCompileTimeOnly: return
  1649. if paramType.isNil:
  1650. genArgNoParam(p, it, r)
  1651. else:
  1652. genArg(p, it, paramType.sym, r)
  1653. inc generated
  1654. proc genPatternCall(p: PProc; n: PNode; pat: string; typ: PType;
  1655. r: var TCompRes) =
  1656. var i = 0
  1657. var j = 1
  1658. r.kind = resExpr
  1659. while i < pat.len:
  1660. case pat[i]
  1661. of '@':
  1662. var generated = 0
  1663. for k in j..<n.len:
  1664. if generated > 0: r.res.add(", ")
  1665. genOtherArg(p, n, k, typ, generated, r)
  1666. inc i
  1667. of '#':
  1668. var generated = 0
  1669. genOtherArg(p, n, j, typ, generated, r)
  1670. inc j
  1671. inc i
  1672. of '\31':
  1673. # unit separator
  1674. r.res.add("#")
  1675. inc i
  1676. of '\29':
  1677. # group separator
  1678. r.res.add("@")
  1679. inc i
  1680. else:
  1681. let start = i
  1682. while i < pat.len:
  1683. if pat[i] notin {'@', '#', '\31', '\29'}: inc(i)
  1684. else: break
  1685. if i - 1 >= start:
  1686. r.res.add(substr(pat, start, i - 1))
  1687. proc genInfixCall(p: PProc, n: PNode, r: var TCompRes) =
  1688. # don't call '$' here for efficiency:
  1689. let f = n[0].sym
  1690. if f.loc.r == "": f.loc.r = mangleName(p.module, f)
  1691. if sfInfixCall in f.flags:
  1692. let pat = $n[0].sym.loc.r
  1693. internalAssert p.config, pat.len > 0
  1694. if pat.contains({'#', '(', '@'}):
  1695. var typ = skipTypes(n[0].typ, abstractInst)
  1696. assert(typ.kind == tyProc)
  1697. genPatternCall(p, n, pat, typ, r)
  1698. return
  1699. if n.len != 1:
  1700. gen(p, n[1], r)
  1701. if r.typ == etyBaseIndex:
  1702. if r.address == "":
  1703. globalError(p.config, n.info, "cannot invoke with infix syntax")
  1704. r.res = "$1[$2]" % [r.address, r.res]
  1705. r.address = ""
  1706. r.typ = etyNone
  1707. r.res.add(".")
  1708. var op: TCompRes = default(TCompRes)
  1709. gen(p, n[0], op)
  1710. r.res.add(op.res)
  1711. genArgs(p, n, r, 2)
  1712. proc genCall(p: PProc, n: PNode, r: var TCompRes) =
  1713. gen(p, n[0], r)
  1714. genArgs(p, n, r)
  1715. if n.typ != nil:
  1716. let t = mapType(n.typ)
  1717. if t == etyBaseIndex:
  1718. let tmp = p.getTemp
  1719. r.address = "($1 = $2, $1)[0]" % [tmp, r.rdLoc]
  1720. r.res = "$1[1]" % [tmp]
  1721. r.tmpLoc = tmp
  1722. r.typ = t
  1723. proc genEcho(p: PProc, n: PNode, r: var TCompRes) =
  1724. let n = n[1].skipConv
  1725. internalAssert p.config, n.kind == nkBracket
  1726. useMagic(p, "toJSStr") # Used in rawEcho
  1727. useMagic(p, "rawEcho")
  1728. r.res.add("rawEcho(")
  1729. for i in 0..<n.len:
  1730. let it = n[i]
  1731. if it.typ.isCompileTimeOnly: continue
  1732. if i > 0: r.res.add(", ")
  1733. genArgNoParam(p, it, r)
  1734. r.res.add(")")
  1735. r.kind = resExpr
  1736. proc putToSeq(s: string, indirect: bool): Rope =
  1737. result = rope(s)
  1738. if indirect: result = "[$1]" % [result]
  1739. proc createVar(p: PProc, typ: PType, indirect: bool): Rope
  1740. proc createRecordVarAux(p: PProc, rec: PNode, excludedFieldIDs: IntSet, output: var Rope) =
  1741. case rec.kind
  1742. of nkRecList:
  1743. for i in 0..<rec.len:
  1744. createRecordVarAux(p, rec[i], excludedFieldIDs, output)
  1745. of nkRecCase:
  1746. createRecordVarAux(p, rec[0], excludedFieldIDs, output)
  1747. for i in 1..<rec.len:
  1748. createRecordVarAux(p, lastSon(rec[i]), excludedFieldIDs, output)
  1749. of nkSym:
  1750. # Do not produce code for void types
  1751. if isEmptyType(rec.sym.typ): return
  1752. if rec.sym.id notin excludedFieldIDs:
  1753. if output.len > 0: output.add(", ")
  1754. output.addf("$#: ", [mangleName(p.module, rec.sym)])
  1755. output.add(createVar(p, rec.sym.typ, false))
  1756. else: internalError(p.config, rec.info, "createRecordVarAux")
  1757. proc createObjInitList(p: PProc, typ: PType, excludedFieldIDs: IntSet, output: var Rope) =
  1758. var t = typ
  1759. if objHasTypeField(t):
  1760. if output.len > 0: output.add(", ")
  1761. output.addf("m_type: $1", [genTypeInfo(p, t)])
  1762. while t != nil:
  1763. t = t.skipTypes(skipPtrs)
  1764. createRecordVarAux(p, t.n, excludedFieldIDs, output)
  1765. t = t[0]
  1766. proc arrayTypeForElemType(conf: ConfigRef; typ: PType): string =
  1767. let typ = typ.skipTypes(abstractRange)
  1768. case typ.kind
  1769. of tyInt, tyInt32: "Int32Array"
  1770. of tyInt16: "Int16Array"
  1771. of tyInt8: "Int8Array"
  1772. of tyInt64:
  1773. if optJsBigInt64 in conf.globalOptions:
  1774. "BigInt64Array"
  1775. else:
  1776. ""
  1777. of tyUInt, tyUInt32: "Uint32Array"
  1778. of tyUInt16: "Uint16Array"
  1779. of tyUInt8, tyChar, tyBool: "Uint8Array"
  1780. of tyUInt64:
  1781. if optJsBigInt64 in conf.globalOptions:
  1782. "BigUint64Array"
  1783. else:
  1784. ""
  1785. of tyFloat32: "Float32Array"
  1786. of tyFloat64, tyFloat: "Float64Array"
  1787. of tyEnum:
  1788. case typ.size
  1789. of 1: "Uint8Array"
  1790. of 2: "Uint16Array"
  1791. of 4: "Uint32Array"
  1792. else: ""
  1793. else: ""
  1794. proc createVar(p: PProc, typ: PType, indirect: bool): Rope =
  1795. var t = skipTypes(typ, abstractInst)
  1796. case t.kind
  1797. of tyInt8..tyInt32, tyUInt8..tyUInt32, tyEnum, tyChar:
  1798. result = putToSeq("0", indirect)
  1799. of tyInt, tyUInt:
  1800. if $t.sym.loc.r == "bigint":
  1801. result = putToSeq("0n", indirect)
  1802. else:
  1803. result = putToSeq("0", indirect)
  1804. of tyInt64, tyUInt64:
  1805. if optJsBigInt64 in p.config.globalOptions:
  1806. result = putToSeq("0n", indirect)
  1807. else:
  1808. result = putToSeq("0", indirect)
  1809. of tyFloat..tyFloat128:
  1810. result = putToSeq("0.0", indirect)
  1811. of tyRange, tyGenericInst, tyAlias, tySink, tyOwned, tyLent:
  1812. result = createVar(p, lastSon(typ), indirect)
  1813. of tySet:
  1814. result = putToSeq("{}", indirect)
  1815. of tyBool:
  1816. result = putToSeq("false", indirect)
  1817. of tyNil:
  1818. result = putToSeq("null", indirect)
  1819. of tyArray:
  1820. let length = toInt(lengthOrd(p.config, t))
  1821. let e = elemType(t)
  1822. let jsTyp = arrayTypeForElemType(p.config, e)
  1823. if jsTyp.len > 0:
  1824. result = "new $1($2)" % [rope(jsTyp), rope(length)]
  1825. elif length > 32:
  1826. useMagic(p, "arrayConstr")
  1827. # XXX: arrayConstr depends on nimCopy. This line shouldn't be necessary.
  1828. useMagic(p, "nimCopy")
  1829. result = "arrayConstr($1, $2, $3)" % [rope(length),
  1830. createVar(p, e, false), genTypeInfo(p, e)]
  1831. else:
  1832. result = rope("[")
  1833. var i = 0
  1834. while i < length:
  1835. if i > 0: result.add(", ")
  1836. result.add(createVar(p, e, false))
  1837. inc(i)
  1838. result.add("]")
  1839. if indirect: result = "[$1]" % [result]
  1840. of tyTuple:
  1841. result = rope("{")
  1842. for i in 0..<t.len:
  1843. if i > 0: result.add(", ")
  1844. result.addf("Field$1: $2", [i.rope,
  1845. createVar(p, t[i], false)])
  1846. result.add("}")
  1847. if indirect: result = "[$1]" % [result]
  1848. of tyObject:
  1849. var initList: Rope = ""
  1850. createObjInitList(p, t, initIntSet(), initList)
  1851. result = ("({$1})") % [initList]
  1852. if indirect: result = "[$1]" % [result]
  1853. of tyVar, tyPtr, tyRef, tyPointer:
  1854. if mapType(p, t) == etyBaseIndex:
  1855. result = putToSeq("[null, 0]", indirect)
  1856. else:
  1857. result = putToSeq("null", indirect)
  1858. of tySequence, tyString:
  1859. result = putToSeq("[]", indirect)
  1860. of tyCstring, tyProc:
  1861. result = putToSeq("null", indirect)
  1862. of tyStatic:
  1863. if t.n != nil:
  1864. result = createVar(p, lastSon t, indirect)
  1865. else:
  1866. internalError(p.config, "createVar: " & $t.kind)
  1867. result = ""
  1868. else:
  1869. internalError(p.config, "createVar: " & $t.kind)
  1870. result = ""
  1871. template returnType: untyped = ""
  1872. proc genVarInit(p: PProc, v: PSym, n: PNode) =
  1873. var
  1874. a: TCompRes = default(TCompRes)
  1875. s: Rope
  1876. varCode: string
  1877. varName = mangleName(p.module, v)
  1878. useReloadingGuard = sfGlobal in v.flags and p.config.hcrOn
  1879. useGlobalPragmas = sfGlobal in v.flags and ({sfPure, sfThread} * v.flags != {})
  1880. if v.constraint.isNil:
  1881. if useReloadingGuard:
  1882. lineF(p, "var $1;$n", varName)
  1883. lineF(p, "if ($1 === undefined) {$n", varName)
  1884. varCode = $varName
  1885. inc p.extraIndent
  1886. elif useGlobalPragmas:
  1887. lineF(p, "if (globalThis.$1 === undefined) {$n", varName)
  1888. varCode = "globalThis." & $varName
  1889. inc p.extraIndent
  1890. else:
  1891. varCode = "var $2"
  1892. else:
  1893. # Is this really a thought through feature? this basically unused
  1894. # feature makes it impossible for almost all format strings in
  1895. # this function to be checked at compile time.
  1896. varCode = v.constraint.strVal
  1897. if n.kind == nkEmpty:
  1898. if not isIndirect(v) and
  1899. v.typ.kind in {tyVar, tyPtr, tyLent, tyRef, tyOwned} and mapType(p, v.typ) == etyBaseIndex:
  1900. lineF(p, "var $1 = null;$n", [varName])
  1901. lineF(p, "var $1_Idx = 0;$n", [varName])
  1902. else:
  1903. line(p, runtimeFormat(varCode & " = $3;$n", [returnType, varName, createVar(p, v.typ, isIndirect(v))]))
  1904. else:
  1905. gen(p, n, a)
  1906. case mapType(p, v.typ)
  1907. of etyObject, etySeq:
  1908. if needsNoCopy(p, n):
  1909. s = a.res
  1910. else:
  1911. useMagic(p, "nimCopy")
  1912. s = "nimCopy(null, $1, $2)" % [a.res, genTypeInfo(p, n.typ)]
  1913. of etyBaseIndex:
  1914. let targetBaseIndex = {sfAddrTaken, sfGlobal} * v.flags == {}
  1915. if a.typ == etyBaseIndex:
  1916. if targetBaseIndex:
  1917. line(p, runtimeFormat(varCode & " = $3, $2_Idx = $4;$n",
  1918. [returnType, v.loc.r, a.address, a.res]))
  1919. else:
  1920. if isIndirect(v):
  1921. line(p, runtimeFormat(varCode & " = [[$3, $4]];$n",
  1922. [returnType, v.loc.r, a.address, a.res]))
  1923. else:
  1924. line(p, runtimeFormat(varCode & " = [$3, $4];$n",
  1925. [returnType, v.loc.r, a.address, a.res]))
  1926. else:
  1927. if targetBaseIndex:
  1928. let tmp = p.getTemp
  1929. lineF(p, "var $1 = $2, $3 = $1[0], $3_Idx = $1[1];$n",
  1930. [tmp, a.res, v.loc.r])
  1931. else:
  1932. line(p, runtimeFormat(varCode & " = $3;$n", [returnType, v.loc.r, a.res]))
  1933. return
  1934. else:
  1935. s = a.res
  1936. if isIndirect(v):
  1937. line(p, runtimeFormat(varCode & " = [$3];$n", [returnType, v.loc.r, s]))
  1938. else:
  1939. line(p, runtimeFormat(varCode & " = $3;$n", [returnType, v.loc.r, s]))
  1940. if useReloadingGuard or useGlobalPragmas:
  1941. dec p.extraIndent
  1942. lineF(p, "}$n")
  1943. proc genVarStmt(p: PProc, n: PNode) =
  1944. for i in 0..<n.len:
  1945. var a = n[i]
  1946. if a.kind != nkCommentStmt:
  1947. if a.kind == nkVarTuple:
  1948. let unpacked = lowerTupleUnpacking(p.module.graph, a, p.module.idgen, p.prc)
  1949. genStmt(p, unpacked)
  1950. else:
  1951. assert(a.kind == nkIdentDefs)
  1952. assert(a[0].kind == nkSym)
  1953. var v = a[0].sym
  1954. if lfNoDecl notin v.loc.flags and sfImportc notin v.flags:
  1955. genLineDir(p, a)
  1956. if sfCompileTime notin v.flags:
  1957. genVarInit(p, v, a[2])
  1958. else:
  1959. # lazy emit, done when it's actually used.
  1960. if v.ast == nil: v.ast = a[2]
  1961. proc genConstant(p: PProc, c: PSym) =
  1962. if lfNoDecl notin c.loc.flags and not p.g.generatedSyms.containsOrIncl(c.id):
  1963. let oldBody = move p.body
  1964. #genLineDir(p, c.astdef)
  1965. genVarInit(p, c, c.astdef)
  1966. p.g.constants.add(p.body)
  1967. p.body = oldBody
  1968. proc genNew(p: PProc, n: PNode) =
  1969. var a: TCompRes = default(TCompRes)
  1970. gen(p, n[1], a)
  1971. var t = skipTypes(n[1].typ, abstractVar)[0]
  1972. if mapType(t) == etyObject:
  1973. lineF(p, "$1 = $2;$n", [a.rdLoc, createVar(p, t, false)])
  1974. elif a.typ == etyBaseIndex:
  1975. lineF(p, "$1 = [$3]; $2 = 0;$n", [a.address, a.res, createVar(p, t, false)])
  1976. else:
  1977. lineF(p, "$1 = [[$2], 0];$n", [a.rdLoc, createVar(p, t, false)])
  1978. proc genNewSeq(p: PProc, n: PNode) =
  1979. var x, y: TCompRes = default(TCompRes)
  1980. gen(p, n[1], x)
  1981. gen(p, n[2], y)
  1982. let t = skipTypes(n[1].typ, abstractVar)[0]
  1983. lineF(p, "$1 = new Array($2); for (var i = 0 ; i < $2 ; ++i) { $1[i] = $3; }", [
  1984. x.rdLoc, y.rdLoc, createVar(p, t, false)])
  1985. proc genOrd(p: PProc, n: PNode, r: var TCompRes) =
  1986. case skipTypes(n[1].typ, abstractVar + abstractRange).kind
  1987. of tyEnum, tyInt..tyInt32, tyUInt..tyUInt32, tyChar: gen(p, n[1], r)
  1988. of tyInt64, tyUInt64:
  1989. if optJsBigInt64 in p.config.globalOptions:
  1990. unaryExpr(p, n, r, "", "Number($1)")
  1991. else: gen(p, n[1], r)
  1992. of tyBool: unaryExpr(p, n, r, "", "($1 ? 1 : 0)")
  1993. else: internalError(p.config, n.info, "genOrd")
  1994. proc genConStrStr(p: PProc, n: PNode, r: var TCompRes) =
  1995. var a: TCompRes = default(TCompRes)
  1996. gen(p, n[1], a)
  1997. r.kind = resExpr
  1998. if skipTypes(n[1].typ, abstractVarRange).kind == tyChar:
  1999. r.res.add("[$1].concat(" % [a.res])
  2000. else:
  2001. r.res.add("($1 || []).concat(" % [a.res])
  2002. for i in 2..<n.len - 1:
  2003. gen(p, n[i], a)
  2004. if skipTypes(n[i].typ, abstractVarRange).kind == tyChar:
  2005. r.res.add("[$1]," % [a.res])
  2006. else:
  2007. r.res.add("$1 || []," % [a.res])
  2008. gen(p, n[^1], a)
  2009. if skipTypes(n[^1].typ, abstractVarRange).kind == tyChar:
  2010. r.res.add("[$1])" % [a.res])
  2011. else:
  2012. r.res.add("$1 || [])" % [a.res])
  2013. proc genReprAux(p: PProc, n: PNode, r: var TCompRes, magic: string, typ: Rope = "") =
  2014. useMagic(p, magic)
  2015. r.res.add(magic & "(")
  2016. var a: TCompRes = default(TCompRes)
  2017. gen(p, n[1], a)
  2018. if magic == "reprAny":
  2019. # the pointer argument in reprAny is expandend to
  2020. # (pointedto, pointer), so we need to fill it
  2021. if a.address.len == 0:
  2022. r.res.add(a.res)
  2023. r.res.add(", null")
  2024. else:
  2025. r.res.add("$1, $2" % [a.address, a.res])
  2026. else:
  2027. r.res.add(a.res)
  2028. if typ != "":
  2029. r.res.add(", ")
  2030. r.res.add(typ)
  2031. r.res.add(")")
  2032. proc genRepr(p: PProc, n: PNode, r: var TCompRes) =
  2033. let t = skipTypes(n[1].typ, abstractVarRange)
  2034. case t.kind
  2035. of tyInt..tyInt64, tyUInt..tyUInt64:
  2036. genReprAux(p, n, r, "reprInt")
  2037. of tyChar:
  2038. genReprAux(p, n, r, "reprChar")
  2039. of tyBool:
  2040. genReprAux(p, n, r, "reprBool")
  2041. of tyFloat..tyFloat128:
  2042. genReprAux(p, n, r, "reprFloat")
  2043. of tyString:
  2044. genReprAux(p, n, r, "reprStr")
  2045. of tyEnum, tyOrdinal:
  2046. genReprAux(p, n, r, "reprEnum", genTypeInfo(p, t))
  2047. of tySet:
  2048. genReprAux(p, n, r, "reprSet", genTypeInfo(p, t))
  2049. of tyEmpty, tyVoid:
  2050. localError(p.config, n.info, "'repr' doesn't support 'void' type")
  2051. of tyPointer:
  2052. genReprAux(p, n, r, "reprPointer")
  2053. of tyOpenArray, tyVarargs:
  2054. genReprAux(p, n, r, "reprJSONStringify")
  2055. else:
  2056. genReprAux(p, n, r, "reprAny", genTypeInfo(p, t))
  2057. r.kind = resExpr
  2058. proc genOf(p: PProc, n: PNode, r: var TCompRes) =
  2059. var x: TCompRes = default(TCompRes)
  2060. let t = skipTypes(n[2].typ,
  2061. abstractVarRange+{tyRef, tyPtr, tyLent, tyTypeDesc, tyOwned})
  2062. gen(p, n[1], x)
  2063. if tfFinal in t.flags:
  2064. r.res = "($1.m_type == $2)" % [x.res, genTypeInfo(p, t)]
  2065. else:
  2066. useMagic(p, "isObj")
  2067. r.res = "isObj($1.m_type, $2)" % [x.res, genTypeInfo(p, t)]
  2068. r.kind = resExpr
  2069. proc genDefault(p: PProc, n: PNode; r: var TCompRes) =
  2070. r.res = createVar(p, n.typ, indirect = false)
  2071. r.kind = resExpr
  2072. proc genReset(p: PProc, n: PNode) =
  2073. var x: TCompRes = default(TCompRes)
  2074. useMagic(p, "genericReset")
  2075. gen(p, n[1], x)
  2076. if x.typ == etyBaseIndex:
  2077. lineF(p, "$1 = null, $2 = 0;$n", [x.address, x.res])
  2078. else:
  2079. let (a, tmp) = maybeMakeTempAssignable(p, n[1], x)
  2080. lineF(p, "$1 = genericReset($3, $2);$n", [a,
  2081. genTypeInfo(p, n[1].typ), tmp])
  2082. proc genMove(p: PProc; n: PNode; r: var TCompRes) =
  2083. var a: TCompRes = default(TCompRes)
  2084. r.kind = resVal
  2085. r.res = p.getTemp()
  2086. gen(p, n[1], a)
  2087. lineF(p, "$1 = $2;$n", [r.rdLoc, a.rdLoc])
  2088. genReset(p, n)
  2089. #lineF(p, "$1 = $2;$n", [dest.rdLoc, src.rdLoc])
  2090. proc genDup(p: PProc; n: PNode; r: var TCompRes) =
  2091. var a: TCompRes = default(TCompRes)
  2092. r.kind = resVal
  2093. r.res = p.getTemp()
  2094. gen(p, n[1], a)
  2095. lineF(p, "$1 = $2;$n", [r.rdLoc, a.rdLoc])
  2096. proc genJSArrayConstr(p: PProc, n: PNode, r: var TCompRes) =
  2097. var a: TCompRes = default(TCompRes)
  2098. r.res = rope("[")
  2099. r.kind = resExpr
  2100. for i in 0 ..< n.len:
  2101. if i > 0: r.res.add(", ")
  2102. gen(p, n[i], a)
  2103. if a.typ == etyBaseIndex:
  2104. r.res.addf("[$1, $2]", [a.address, a.res])
  2105. else:
  2106. if not needsNoCopy(p, n[i]):
  2107. let typ = n[i].typ.skipTypes(abstractInst)
  2108. useMagic(p, "nimCopy")
  2109. a.res = "nimCopy(null, $1, $2)" % [a.rdLoc, genTypeInfo(p, typ)]
  2110. r.res.add(a.res)
  2111. r.res.add("]")
  2112. proc genMagic(p: PProc, n: PNode, r: var TCompRes) =
  2113. var
  2114. a: TCompRes
  2115. line, filen: Rope
  2116. var op = n[0].sym.magic
  2117. case op
  2118. of mOr: genOr(p, n[1], n[2], r)
  2119. of mAnd: genAnd(p, n[1], n[2], r)
  2120. of mAddI..mStrToStr: arith(p, n, r, op)
  2121. of mRepr: genRepr(p, n, r)
  2122. of mSwap: genSwap(p, n)
  2123. of mAppendStrCh:
  2124. binaryExpr(p, n, r, "addChar",
  2125. "addChar($1, $2);")
  2126. of mAppendStrStr:
  2127. var lhs, rhs: TCompRes = default(TCompRes)
  2128. gen(p, n[1], lhs)
  2129. gen(p, n[2], rhs)
  2130. if skipTypes(n[1].typ, abstractVarRange).kind == tyCstring:
  2131. let (b, tmp) = maybeMakeTemp(p, n[2], rhs)
  2132. r.res = "if (null != $1) { if (null == $2) $2 = $3; else $2 += $3; }" %
  2133. [b, lhs.rdLoc, tmp]
  2134. else:
  2135. let (a, tmp) = maybeMakeTemp(p, n[1], lhs)
  2136. r.res = "$1.push.apply($3, $2);" % [a, rhs.rdLoc, tmp]
  2137. r.kind = resExpr
  2138. of mAppendSeqElem:
  2139. var x, y: TCompRes = default(TCompRes)
  2140. gen(p, n[1], x)
  2141. gen(p, n[2], y)
  2142. if mapType(n[2].typ) == etyBaseIndex:
  2143. let c = "[$1, $2]" % [y.address, y.res]
  2144. r.res = "$1.push($2);" % [x.rdLoc, c]
  2145. elif needsNoCopy(p, n[2]):
  2146. r.res = "$1.push($2);" % [x.rdLoc, y.rdLoc]
  2147. else:
  2148. useMagic(p, "nimCopy")
  2149. let c = getTemp(p, defineInLocals=false)
  2150. lineF(p, "var $1 = nimCopy(null, $2, $3);$n",
  2151. [c, y.rdLoc, genTypeInfo(p, n[2].typ)])
  2152. r.res = "$1.push($2);" % [x.rdLoc, c]
  2153. r.kind = resExpr
  2154. of mConStrStr:
  2155. genConStrStr(p, n, r)
  2156. of mEqStr:
  2157. binaryExpr(p, n, r, "eqStrings", "eqStrings($1, $2)")
  2158. of mLeStr:
  2159. binaryExpr(p, n, r, "cmpStrings", "(cmpStrings($1, $2) <= 0)")
  2160. of mLtStr:
  2161. binaryExpr(p, n, r, "cmpStrings", "(cmpStrings($1, $2) < 0)")
  2162. of mIsNil:
  2163. # we want to accept undefined, so we ==
  2164. if mapType(n[1].typ) != etyBaseIndex:
  2165. unaryExpr(p, n, r, "", "($1 == null)")
  2166. else:
  2167. var x: TCompRes = default(TCompRes)
  2168. gen(p, n[1], x)
  2169. r.res = "($# == null && $# === 0)" % [x.address, x.res]
  2170. of mEnumToStr: genRepr(p, n, r)
  2171. of mNew, mNewFinalize: genNew(p, n)
  2172. of mChr: gen(p, n[1], r)
  2173. of mArrToSeq:
  2174. # only array literals doesn't need copy
  2175. if n[1].kind == nkBracket:
  2176. genJSArrayConstr(p, n[1], r)
  2177. else:
  2178. var x: TCompRes = default(TCompRes)
  2179. gen(p, n[1], x)
  2180. useMagic(p, "nimCopy")
  2181. r.res = "nimCopy(null, $1, $2)" % [x.rdLoc, genTypeInfo(p, n.typ)]
  2182. of mOpenArrayToSeq:
  2183. genCall(p, n, r)
  2184. of mDestroy, mTrace: discard "ignore calls to the default destructor"
  2185. of mOrd: genOrd(p, n, r)
  2186. of mLengthStr, mLengthSeq, mLengthOpenArray, mLengthArray:
  2187. var x: TCompRes = default(TCompRes)
  2188. gen(p, n[1], x)
  2189. if skipTypes(n[1].typ, abstractInst).kind == tyCstring:
  2190. let (a, tmp) = maybeMakeTemp(p, n[1], x)
  2191. r.res = "(($1) == null ? 0 : ($2).length)" % [a, tmp]
  2192. else:
  2193. r.res = "($1).length" % [x.rdLoc]
  2194. r.kind = resExpr
  2195. of mHigh:
  2196. var x: TCompRes = default(TCompRes)
  2197. gen(p, n[1], x)
  2198. if skipTypes(n[1].typ, abstractInst).kind == tyCstring:
  2199. let (a, tmp) = maybeMakeTemp(p, n[1], x)
  2200. r.res = "(($1) == null ? -1 : ($2).length - 1)" % [a, tmp]
  2201. else:
  2202. r.res = "($1).length - 1" % [x.rdLoc]
  2203. r.kind = resExpr
  2204. of mInc:
  2205. let typ = n[1].typ.skipTypes(abstractVarRange)
  2206. case typ.kind
  2207. of tyUInt..tyUInt32:
  2208. binaryUintExpr(p, n, r, "+", true)
  2209. of tyUInt64:
  2210. if optJsBigInt64 in p.config.globalOptions:
  2211. binaryExpr(p, n, r, "", "$1 = BigInt.asUintN(64, $3 + BigInt($2))", true)
  2212. else: binaryUintExpr(p, n, r, "+", true)
  2213. elif typ.kind == tyInt64 and optJsBigInt64 in p.config.globalOptions:
  2214. if optOverflowCheck notin p.options:
  2215. binaryExpr(p, n, r, "", "$1 = BigInt.asIntN(64, $3 + BigInt($2))", true)
  2216. else: binaryExpr(p, n, r, "addInt64", "$1 = addInt64($3, BigInt($2))", true)
  2217. else:
  2218. if optOverflowCheck notin p.options: binaryExpr(p, n, r, "", "$1 += $2")
  2219. else: binaryExpr(p, n, r, "addInt", "$1 = addInt($3, $2)", true)
  2220. of ast.mDec:
  2221. let typ = n[1].typ.skipTypes(abstractVarRange)
  2222. case typ.kind
  2223. of tyUInt..tyUInt32:
  2224. binaryUintExpr(p, n, r, "-", true)
  2225. of tyUInt64:
  2226. if optJsBigInt64 in p.config.globalOptions:
  2227. binaryExpr(p, n, r, "", "$1 = BigInt.asUintN(64, $3 - BigInt($2))", true)
  2228. else: binaryUintExpr(p, n, r, "-", true)
  2229. elif typ.kind == tyInt64 and optJsBigInt64 in p.config.globalOptions:
  2230. if optOverflowCheck notin p.options:
  2231. binaryExpr(p, n, r, "", "$1 = BigInt.asIntN(64, $3 - BigInt($2))", true)
  2232. else: binaryExpr(p, n, r, "subInt64", "$1 = subInt64($3, BigInt($2))", true)
  2233. else:
  2234. if optOverflowCheck notin p.options: binaryExpr(p, n, r, "", "$1 -= $2")
  2235. else: binaryExpr(p, n, r, "subInt", "$1 = subInt($3, $2)", true)
  2236. of mSetLengthStr:
  2237. binaryExpr(p, n, r, "mnewString",
  2238. """if ($1.length < $2) { for (var i = $3.length; i < $4; ++i) $3.push(0); }
  2239. else {$3.length = $4; }""")
  2240. of mSetLengthSeq:
  2241. var x, y: TCompRes = default(TCompRes)
  2242. gen(p, n[1], x)
  2243. gen(p, n[2], y)
  2244. let t = skipTypes(n[1].typ, abstractVar)[0]
  2245. let (a, tmp) = maybeMakeTemp(p, n[1], x)
  2246. let (b, tmp2) = maybeMakeTemp(p, n[2], y)
  2247. r.res = """if ($1.length < $2) { for (var i = $4.length ; i < $5 ; ++i) $4.push($3); }
  2248. else { $4.length = $5; }""" % [a, b, createVar(p, t, false), tmp, tmp2]
  2249. r.kind = resExpr
  2250. of mCard: unaryExpr(p, n, r, "SetCard", "SetCard($1)")
  2251. of mLtSet: binaryExpr(p, n, r, "SetLt", "SetLt($1, $2)")
  2252. of mLeSet: binaryExpr(p, n, r, "SetLe", "SetLe($1, $2)")
  2253. of mEqSet: binaryExpr(p, n, r, "SetEq", "SetEq($1, $2)")
  2254. of mMulSet: binaryExpr(p, n, r, "SetMul", "SetMul($1, $2)")
  2255. of mPlusSet: binaryExpr(p, n, r, "SetPlus", "SetPlus($1, $2)")
  2256. of mMinusSet: binaryExpr(p, n, r, "SetMinus", "SetMinus($1, $2)")
  2257. of mIncl: binaryExpr(p, n, r, "", "$1[$2] = true")
  2258. of mExcl: binaryExpr(p, n, r, "", "delete $1[$2]")
  2259. of mInSet:
  2260. binaryExpr(p, n, r, "", "($1[$2] != undefined)")
  2261. of mNewSeq: genNewSeq(p, n)
  2262. of mNewSeqOfCap: unaryExpr(p, n, r, "", "[]")
  2263. of mOf: genOf(p, n, r)
  2264. of mDefault, mZeroDefault: genDefault(p, n, r)
  2265. of mReset, mWasMoved: genReset(p, n)
  2266. of mEcho: genEcho(p, n, r)
  2267. of mNLen..mNError, mSlurp, mStaticExec:
  2268. localError(p.config, n.info, errXMustBeCompileTime % n[0].sym.name.s)
  2269. of mNewString: unaryExpr(p, n, r, "mnewString", "mnewString($1)")
  2270. of mNewStringOfCap:
  2271. unaryExpr(p, n, r, "mnewString", "mnewString(0)")
  2272. of mDotDot:
  2273. genProcForSymIfNeeded(p, n[0].sym)
  2274. genCall(p, n, r)
  2275. of mParseBiggestFloat:
  2276. useMagic(p, "nimParseBiggestFloat")
  2277. genCall(p, n, r)
  2278. of mSlice:
  2279. # arr.slice([begin[, end]]): 'end' is exclusive
  2280. var x, y, z: TCompRes = default(TCompRes)
  2281. gen(p, n[1], x)
  2282. gen(p, n[2], y)
  2283. gen(p, n[3], z)
  2284. r.res = "($1.slice($2, $3 + 1))" % [x.rdLoc, y.rdLoc, z.rdLoc]
  2285. r.kind = resExpr
  2286. of mMove:
  2287. genMove(p, n, r)
  2288. of mDup:
  2289. genDup(p, n, r)
  2290. of mEnsureMove:
  2291. gen(p, n[1], r)
  2292. else:
  2293. genCall(p, n, r)
  2294. #else internalError(p.config, e.info, 'genMagic: ' + magicToStr[op]);
  2295. proc genSetConstr(p: PProc, n: PNode, r: var TCompRes) =
  2296. var
  2297. a, b: TCompRes = default(TCompRes)
  2298. useMagic(p, "setConstr")
  2299. r.res = rope("setConstr(")
  2300. r.kind = resExpr
  2301. for i in 0..<n.len:
  2302. if i > 0: r.res.add(", ")
  2303. var it = n[i]
  2304. if it.kind == nkRange:
  2305. gen(p, it[0], a)
  2306. gen(p, it[1], b)
  2307. if it[0].typ.kind == tyBool:
  2308. r.res.addf("$1, $2", [a.res, b.res])
  2309. else:
  2310. r.res.addf("[$1, $2]", [a.res, b.res])
  2311. else:
  2312. gen(p, it, a)
  2313. r.res.add(a.res)
  2314. r.res.add(")")
  2315. # emit better code for constant sets:
  2316. if isDeepConstExpr(n):
  2317. inc(p.g.unique)
  2318. let tmp = rope("ConstSet") & rope(p.g.unique)
  2319. p.g.constants.addf("var $1 = $2;$n", [tmp, r.res])
  2320. r.res = tmp
  2321. proc genArrayConstr(p: PProc, n: PNode, r: var TCompRes) =
  2322. ## Constructs array or sequence.
  2323. ## Nim array of uint8..uint32, int8..int32 maps to JS typed arrays.
  2324. ## Nim sequence maps to JS array.
  2325. var t = skipTypes(n.typ, abstractInst)
  2326. let e = elemType(t)
  2327. let jsTyp = arrayTypeForElemType(p.config, e)
  2328. if skipTypes(n.typ, abstractVarRange).kind != tySequence and jsTyp.len > 0:
  2329. # generate typed array
  2330. # for example Nim generates `new Uint8Array([1, 2, 3])` for `[byte(1), 2, 3]`
  2331. # TODO use `set` or loop to initialize typed array which improves performances in some situations
  2332. var a: TCompRes = default(TCompRes)
  2333. r.res = "new $1([" % [rope(jsTyp)]
  2334. r.kind = resExpr
  2335. for i in 0 ..< n.len:
  2336. if i > 0: r.res.add(", ")
  2337. gen(p, n[i], a)
  2338. r.res.add(a.res)
  2339. r.res.add("])")
  2340. else:
  2341. genJSArrayConstr(p, n, r)
  2342. proc genTupleConstr(p: PProc, n: PNode, r: var TCompRes) =
  2343. var a: TCompRes = default(TCompRes)
  2344. r.res = rope("{")
  2345. r.kind = resExpr
  2346. for i in 0..<n.len:
  2347. if i > 0: r.res.add(", ")
  2348. var it = n[i]
  2349. if it.kind == nkExprColonExpr: it = it[1]
  2350. gen(p, it, a)
  2351. let typ = it.typ.skipTypes(abstractInst)
  2352. if a.typ == etyBaseIndex:
  2353. r.res.addf("Field$#: [$#, $#]", [i.rope, a.address, a.res])
  2354. else:
  2355. if not needsNoCopy(p, it):
  2356. useMagic(p, "nimCopy")
  2357. a.res = "nimCopy(null, $1, $2)" % [a.rdLoc, genTypeInfo(p, typ)]
  2358. r.res.addf("Field$#: $#", [i.rope, a.res])
  2359. r.res.add("}")
  2360. proc genObjConstr(p: PProc, n: PNode, r: var TCompRes) =
  2361. var a: TCompRes = default(TCompRes)
  2362. r.kind = resExpr
  2363. var initList : Rope = ""
  2364. var fieldIDs = initIntSet()
  2365. let nTyp = n.typ.skipTypes(abstractInst)
  2366. for i in 1..<n.len:
  2367. if i > 1: initList.add(", ")
  2368. var it = n[i]
  2369. internalAssert p.config, it.kind == nkExprColonExpr
  2370. let val = it[1]
  2371. gen(p, val, a)
  2372. var f = it[0].sym
  2373. if f.loc.r == "": f.loc.r = mangleName(p.module, f)
  2374. fieldIDs.incl(lookupFieldAgain(n.typ.skipTypes({tyDistinct}), f).id)
  2375. let typ = val.typ.skipTypes(abstractInst)
  2376. if a.typ == etyBaseIndex:
  2377. initList.addf("$#: [$#, $#]", [f.loc.r, a.address, a.res])
  2378. else:
  2379. if not needsNoCopy(p, val):
  2380. useMagic(p, "nimCopy")
  2381. a.res = "nimCopy(null, $1, $2)" % [a.rdLoc, genTypeInfo(p, typ)]
  2382. initList.addf("$#: $#", [f.loc.r, a.res])
  2383. let t = skipTypes(n.typ, abstractInst + skipPtrs)
  2384. createObjInitList(p, t, fieldIDs, initList)
  2385. r.res = ("{$1}") % [initList]
  2386. proc genConv(p: PProc, n: PNode, r: var TCompRes) =
  2387. var dest = skipTypes(n.typ, abstractVarRange)
  2388. var src = skipTypes(n[1].typ, abstractVarRange)
  2389. gen(p, n[1], r)
  2390. if dest.kind == src.kind:
  2391. # no-op conversion
  2392. return
  2393. let toInt = (dest.kind in tyInt..tyInt32)
  2394. let fromInt = (src.kind in tyInt..tyInt32)
  2395. let toUint = (dest.kind in tyUInt..tyUInt32)
  2396. let fromUint = (src.kind in tyUInt..tyUInt32)
  2397. if toUint and (fromInt or fromUint):
  2398. let trimmer = unsignedTrimmer(dest.size)
  2399. r.res = "($1 $2)" % [r.res, trimmer]
  2400. elif dest.kind == tyBool:
  2401. r.res = "(!!($1))" % [r.res]
  2402. r.kind = resExpr
  2403. elif toInt:
  2404. if src.kind in {tyInt64, tyUInt64} and optJsBigInt64 in p.config.globalOptions:
  2405. r.res = "Number($1)" % [r.res]
  2406. else:
  2407. r.res = "(($1) | 0)" % [r.res]
  2408. elif dest.kind == tyInt64 and optJsBigInt64 in p.config.globalOptions:
  2409. if fromInt or fromUint or src.kind in {tyBool, tyChar, tyEnum}:
  2410. r.res = "BigInt($1)" % [r.res]
  2411. elif src.kind in {tyFloat..tyFloat64}:
  2412. r.res = "BigInt(Math.trunc($1))" % [r.res]
  2413. elif src.kind == tyUInt64:
  2414. r.res = "BigInt.asIntN(64, $1)" % [r.res]
  2415. elif dest.kind == tyUInt64 and optJsBigInt64 in p.config.globalOptions:
  2416. if fromUint or src.kind in {tyBool, tyChar, tyEnum}:
  2417. r.res = "BigInt($1)" % [r.res]
  2418. elif fromInt: # could be negative
  2419. r.res = "BigInt.asUintN(64, BigInt($1))" % [r.res]
  2420. elif src.kind in {tyFloat..tyFloat64}:
  2421. r.res = "BigInt.asUintN(64, BigInt(Math.trunc($1)))" % [r.res]
  2422. elif src.kind == tyInt64:
  2423. r.res = "BigInt.asUintN(64, $1)" % [r.res]
  2424. elif toUint or dest.kind in tyFloat..tyFloat64:
  2425. if src.kind in {tyInt64, tyUInt64} and optJsBigInt64 in p.config.globalOptions:
  2426. r.res = "Number($1)" % [r.res]
  2427. else:
  2428. # TODO: What types must we handle here?
  2429. discard
  2430. proc upConv(p: PProc, n: PNode, r: var TCompRes) =
  2431. gen(p, n[0], r) # XXX
  2432. proc genRangeChck(p: PProc, n: PNode, r: var TCompRes, magic: string) =
  2433. var a, b: TCompRes = default(TCompRes)
  2434. gen(p, n[0], r)
  2435. let src = skipTypes(n[0].typ, abstractVarRange)
  2436. let dest = skipTypes(n.typ, abstractVarRange)
  2437. if optRangeCheck notin p.options:
  2438. return
  2439. elif dest.kind in {tyUInt..tyUInt64} and checkUnsignedConversions notin p.config.legacyFeatures:
  2440. if src.kind in {tyInt64, tyUInt64} and optJsBigInt64 in p.config.globalOptions:
  2441. r.res = "BigInt.asUintN($1, $2)" % [$(dest.size * 8), r.res]
  2442. else:
  2443. r.res = "BigInt.asUintN($1, BigInt($2))" % [$(dest.size * 8), r.res]
  2444. if not (dest.kind == tyUInt64 and optJsBigInt64 in p.config.globalOptions):
  2445. r.res = "Number($1)" % [r.res]
  2446. else:
  2447. if src.kind in {tyInt64, tyUInt64} and dest.kind notin {tyInt64, tyUInt64} and optJsBigInt64 in p.config.globalOptions:
  2448. # we do a range check anyway, so it's ok if the number gets rounded
  2449. r.res = "Number($1)" % [r.res]
  2450. gen(p, n[1], a)
  2451. gen(p, n[2], b)
  2452. useMagic(p, "chckRange")
  2453. r.res = "chckRange($1, $2, $3)" % [r.res, a.res, b.res]
  2454. r.kind = resExpr
  2455. proc convStrToCStr(p: PProc, n: PNode, r: var TCompRes) =
  2456. # we do an optimization here as this is likely to slow down
  2457. # much of the code otherwise:
  2458. if n[0].kind == nkCStringToString:
  2459. gen(p, n[0][0], r)
  2460. else:
  2461. gen(p, n[0], r)
  2462. if r.res == "": internalError(p.config, n.info, "convStrToCStr")
  2463. useMagic(p, "toJSStr")
  2464. r.res = "toJSStr($1)" % [r.res]
  2465. r.kind = resExpr
  2466. proc convCStrToStr(p: PProc, n: PNode, r: var TCompRes) =
  2467. # we do an optimization here as this is likely to slow down
  2468. # much of the code otherwise:
  2469. if n[0].kind == nkStringToCString:
  2470. gen(p, n[0][0], r)
  2471. else:
  2472. gen(p, n[0], r)
  2473. if r.res == "": internalError(p.config, n.info, "convCStrToStr")
  2474. useMagic(p, "cstrToNimstr")
  2475. r.res = "cstrToNimstr($1)" % [r.res]
  2476. r.kind = resExpr
  2477. proc genReturnStmt(p: PProc, n: PNode) =
  2478. if p.procDef == nil: internalError(p.config, n.info, "genReturnStmt")
  2479. p.beforeRetNeeded = true
  2480. if n[0].kind != nkEmpty:
  2481. genStmt(p, n[0])
  2482. else:
  2483. genLineDir(p, n)
  2484. lineF(p, "break BeforeRet;$n", [])
  2485. proc frameCreate(p: PProc; procname, filename: Rope): Rope =
  2486. const frameFmt =
  2487. "var F = {procname: $1, prev: framePtr, filename: $2, line: 0};$n"
  2488. result = p.indentLine(frameFmt % [procname, filename])
  2489. result.add p.indentLine(ropes.`%`("framePtr = F;$n", []))
  2490. proc frameDestroy(p: PProc): Rope =
  2491. result = p.indentLine rope(("framePtr = F.prev;") & "\L")
  2492. proc genProcBody(p: PProc, prc: PSym): Rope =
  2493. if hasFrameInfo(p):
  2494. result = frameCreate(p,
  2495. makeJSString(prc.owner.name.s & '.' & prc.name.s),
  2496. makeJSString(toFilenameOption(p.config, prc.info.fileIndex, foStacktrace)))
  2497. else:
  2498. result = ""
  2499. if p.beforeRetNeeded:
  2500. result.add p.indentLine("BeforeRet: {\n")
  2501. result.add p.body
  2502. result.add p.indentLine("};\n")
  2503. else:
  2504. result.add(p.body)
  2505. if prc.typ.callConv == ccSysCall:
  2506. result = ("try {$n$1} catch (e) {$n" &
  2507. " alert(\"Unhandled exception:\\n\" + e.message + \"\\n\"$n}") % [result]
  2508. if hasFrameInfo(p):
  2509. result.add(frameDestroy(p))
  2510. proc optionalLine(p: Rope): Rope =
  2511. if p == "":
  2512. return ""
  2513. else:
  2514. return p & "\L"
  2515. proc genProc(oldProc: PProc, prc: PSym): Rope =
  2516. ## Generate a JS procedure ('function').
  2517. result = ""
  2518. var
  2519. resultSym: PSym
  2520. a: TCompRes = default(TCompRes)
  2521. #if gVerbosity >= 3:
  2522. # echo "BEGIN generating code for: " & prc.name.s
  2523. var p = newProc(oldProc.g, oldProc.module, prc.ast, prc.options)
  2524. p.up = oldProc
  2525. var returnStmt: Rope = ""
  2526. var resultAsgn: Rope = ""
  2527. var name = mangleName(p.module, prc)
  2528. let header = generateHeader(p, prc.typ)
  2529. if prc.typ[0] != nil and sfPure notin prc.flags:
  2530. resultSym = prc.ast[resultPos].sym
  2531. let mname = mangleName(p.module, resultSym)
  2532. # otherwise uses "fat pointers"
  2533. let useRawPointer = not isIndirect(resultSym) and
  2534. resultSym.typ.kind in {tyVar, tyPtr, tyLent, tyRef, tyOwned} and
  2535. mapType(p, resultSym.typ) == etyBaseIndex
  2536. if useRawPointer:
  2537. resultAsgn = p.indentLine(("var $# = null;$n") % [mname])
  2538. resultAsgn.add p.indentLine("var $#_Idx = 0;$n" % [mname])
  2539. else:
  2540. let resVar = createVar(p, resultSym.typ, isIndirect(resultSym))
  2541. resultAsgn = p.indentLine(("var $# = $#;$n") % [mname, resVar])
  2542. gen(p, prc.ast[resultPos], a)
  2543. if mapType(p, resultSym.typ) == etyBaseIndex:
  2544. returnStmt = "return [$#, $#];$n" % [a.address, a.res]
  2545. else:
  2546. returnStmt = "return $#;$n" % [a.res]
  2547. var transformedBody = transformBody(p.module.graph, p.module.idgen, prc, dontUseCache)
  2548. if sfInjectDestructors in prc.flags:
  2549. transformedBody = injectDestructorCalls(p.module.graph, p.module.idgen, prc, transformedBody)
  2550. p.nested: genStmt(p, transformedBody)
  2551. if optLineDir in p.config.options:
  2552. result = lineDir(p.config, prc.info, toLinenumber(prc.info))
  2553. var def: Rope
  2554. if not prc.constraint.isNil:
  2555. def = runtimeFormat(prc.constraint.strVal & " {$n$#$#$#$#$#",
  2556. [ returnType,
  2557. name,
  2558. header,
  2559. optionalLine(p.globals),
  2560. optionalLine(p.locals),
  2561. optionalLine(resultAsgn),
  2562. optionalLine(genProcBody(p, prc)),
  2563. optionalLine(p.indentLine(returnStmt))])
  2564. else:
  2565. # if optLineDir in p.config.options:
  2566. # result.add("\L")
  2567. if p.config.hcrOn:
  2568. # Here, we introduce thunks that create the equivalent of a jump table
  2569. # for all global functions, because references to them may be stored
  2570. # in JavaScript variables. The added indirection ensures that such
  2571. # references will end up calling the reloaded code.
  2572. var thunkName = name
  2573. name = name & "IMLP"
  2574. result.add("\Lfunction $#() { return $#.apply(this, arguments); }$n" %
  2575. [thunkName, name])
  2576. def = "\Lfunction $#($#) {$n$#$#$#$#$#" %
  2577. [ name,
  2578. header,
  2579. optionalLine(p.globals),
  2580. optionalLine(p.locals),
  2581. optionalLine(resultAsgn),
  2582. optionalLine(genProcBody(p, prc)),
  2583. optionalLine(p.indentLine(returnStmt))]
  2584. dec p.extraIndent
  2585. result.add p.indentLine(def)
  2586. result.add p.indentLine("}\n")
  2587. #if gVerbosity >= 3:
  2588. # echo "END generated code for: " & prc.name.s
  2589. proc genStmt(p: PProc, n: PNode) =
  2590. var r: TCompRes = default(TCompRes)
  2591. gen(p, n, r)
  2592. if r.res != "": lineF(p, "$#;$n", [r.res])
  2593. proc genPragma(p: PProc, n: PNode) =
  2594. for i in 0..<n.len:
  2595. let it = n[i]
  2596. case whichPragma(it)
  2597. of wEmit: genAsmOrEmitStmt(p, it[1])
  2598. of wPush:
  2599. processPushBackendOption(p.optionsStack, p.options, n, i+1)
  2600. of wPop:
  2601. processPopBackendOption(p.optionsStack, p.options)
  2602. else: discard
  2603. proc genCast(p: PProc, n: PNode, r: var TCompRes) =
  2604. var dest = skipTypes(n.typ, abstractVarRange)
  2605. var src = skipTypes(n[1].typ, abstractVarRange)
  2606. gen(p, n[1], r)
  2607. if dest.kind == src.kind:
  2608. # no-op conversion
  2609. return
  2610. let toInt = (dest.kind in tyInt..tyInt32)
  2611. let toUint = (dest.kind in tyUInt..tyUInt32)
  2612. let fromInt = (src.kind in tyInt..tyInt32)
  2613. let fromUint = (src.kind in tyUInt..tyUInt32)
  2614. if toUint:
  2615. if fromInt or fromUint:
  2616. r.res = "Number(BigInt.asUintN($1, BigInt($2)))" % [$(dest.size * 8), r.res]
  2617. elif src.kind in {tyInt64, tyUInt64} and optJsBigInt64 in p.config.globalOptions:
  2618. r.res = "Number(BigInt.asUintN($1, $2))" % [$(dest.size * 8), r.res]
  2619. elif toInt:
  2620. if fromInt or fromUint:
  2621. r.res = "Number(BigInt.asIntN($1, BigInt($2)))" % [$(dest.size * 8), r.res]
  2622. elif src.kind in {tyInt64, tyUInt64} and optJsBigInt64 in p.config.globalOptions:
  2623. r.res = "Number(BigInt.asIntN($1, $2))" % [$(dest.size * 8), r.res]
  2624. elif dest.kind == tyInt64 and optJsBigInt64 in p.config.globalOptions:
  2625. if fromInt or fromUint or src.kind in {tyBool, tyChar, tyEnum}:
  2626. r.res = "BigInt($1)" % [r.res]
  2627. elif src.kind in {tyFloat..tyFloat64}:
  2628. r.res = "BigInt(Math.trunc($1))" % [r.res]
  2629. elif src.kind == tyUInt64:
  2630. r.res = "BigInt.asIntN(64, $1)" % [r.res]
  2631. elif dest.kind == tyUInt64 and optJsBigInt64 in p.config.globalOptions:
  2632. if fromUint or src.kind in {tyBool, tyChar, tyEnum}:
  2633. r.res = "BigInt($1)" % [r.res]
  2634. elif fromInt: # could be negative
  2635. r.res = "BigInt.asUintN(64, BigInt($1))" % [r.res]
  2636. elif src.kind in {tyFloat..tyFloat64}:
  2637. r.res = "BigInt.asUintN(64, BigInt(Math.trunc($1)))" % [r.res]
  2638. elif src.kind == tyInt64:
  2639. r.res = "BigInt.asUintN(64, $1)" % [r.res]
  2640. elif dest.kind in tyFloat..tyFloat64:
  2641. if src.kind in {tyInt64, tyUInt64} and optJsBigInt64 in p.config.globalOptions:
  2642. r.res = "Number($1)" % [r.res]
  2643. elif (src.kind == tyPtr and mapType(p, src) == etyObject) and dest.kind == tyPointer:
  2644. r.address = r.res
  2645. r.res = "null"
  2646. r.typ = etyBaseIndex
  2647. elif (dest.kind == tyPtr and mapType(p, dest) == etyObject) and src.kind == tyPointer:
  2648. r.res = r.address
  2649. r.typ = etyObject
  2650. proc gen(p: PProc, n: PNode, r: var TCompRes) =
  2651. r.typ = etyNone
  2652. if r.kind != resCallee: r.kind = resNone
  2653. #r.address = ""
  2654. r.res = ""
  2655. case n.kind
  2656. of nkSym:
  2657. genSym(p, n, r)
  2658. of nkCharLit..nkUInt64Lit:
  2659. case n.typ.skipTypes(abstractVarRange).kind
  2660. of tyBool:
  2661. r.res = if n.intVal == 0: rope"false" else: rope"true"
  2662. of tyUInt64:
  2663. r.res = rope($cast[BiggestUInt](n.intVal))
  2664. if optJsBigInt64 in p.config.globalOptions:
  2665. r.res.add('n')
  2666. of tyInt64:
  2667. let wrap = n.intVal < 0 # wrap negative integers with parens
  2668. if wrap: r.res.add '('
  2669. r.res.addInt n.intVal
  2670. if optJsBigInt64 in p.config.globalOptions:
  2671. r.res.add('n')
  2672. if wrap: r.res.add ')'
  2673. else:
  2674. let wrap = n.intVal < 0 # wrap negative integers with parens
  2675. if wrap: r.res.add '('
  2676. r.res.addInt n.intVal
  2677. if wrap: r.res.add ')'
  2678. r.kind = resExpr
  2679. of nkNilLit:
  2680. if isEmptyType(n.typ):
  2681. discard
  2682. elif mapType(p, n.typ) == etyBaseIndex:
  2683. r.typ = etyBaseIndex
  2684. r.address = rope"null"
  2685. r.res = rope"0"
  2686. r.kind = resExpr
  2687. else:
  2688. r.res = rope"null"
  2689. r.kind = resExpr
  2690. of nkStrLit..nkTripleStrLit:
  2691. if skipTypes(n.typ, abstractVarRange).kind == tyString:
  2692. if n.strVal.len <= 64:
  2693. r.res = makeJsNimStrLit(n.strVal)
  2694. else:
  2695. useMagic(p, "makeNimstrLit")
  2696. r.res = "makeNimstrLit($1)" % [makeJSString(n.strVal)]
  2697. else:
  2698. r.res = makeJSString(n.strVal, false)
  2699. r.kind = resExpr
  2700. of nkFloatLit..nkFloat64Lit:
  2701. let f = n.floatVal
  2702. case classify(f)
  2703. of fcNan:
  2704. if signbit(f):
  2705. r.res = rope"-NaN"
  2706. else:
  2707. r.res = rope"NaN"
  2708. of fcNegZero:
  2709. r.res = rope"-0.0"
  2710. of fcZero:
  2711. r.res = rope"0.0"
  2712. of fcInf:
  2713. r.res = rope"Infinity"
  2714. of fcNegInf:
  2715. r.res = rope"-Infinity"
  2716. else:
  2717. if n.typ.skipTypes(abstractVarRange).kind == tyFloat32:
  2718. r.res.addFloatRoundtrip(f.float32)
  2719. else:
  2720. r.res.addFloatRoundtrip(f)
  2721. r.kind = resExpr
  2722. of nkCallKinds:
  2723. if isEmptyType(n.typ):
  2724. genLineDir(p, n)
  2725. if (n[0].kind == nkSym) and (n[0].sym.magic != mNone):
  2726. genMagic(p, n, r)
  2727. elif n[0].kind == nkSym and sfInfixCall in n[0].sym.flags and
  2728. n.len >= 1:
  2729. genInfixCall(p, n, r)
  2730. else:
  2731. genCall(p, n, r)
  2732. of nkClosure: gen(p, n[0], r)
  2733. of nkCurly: genSetConstr(p, n, r)
  2734. of nkBracket: genArrayConstr(p, n, r)
  2735. of nkPar, nkTupleConstr: genTupleConstr(p, n, r)
  2736. of nkObjConstr: genObjConstr(p, n, r)
  2737. of nkHiddenStdConv, nkHiddenSubConv, nkConv: genConv(p, n, r)
  2738. of nkAddr, nkHiddenAddr:
  2739. if n.typ.kind in {tyLent}:
  2740. gen(p, n[0], r)
  2741. else:
  2742. genAddr(p, n, r)
  2743. of nkDerefExpr, nkHiddenDeref:
  2744. if n.typ.kind in {tyLent}:
  2745. gen(p, n[0], r)
  2746. else:
  2747. genDeref(p, n, r)
  2748. of nkBracketExpr: genArrayAccess(p, n, r)
  2749. of nkDotExpr: genFieldAccess(p, n, r)
  2750. of nkCheckedFieldExpr: genCheckedFieldOp(p, n, nil, r)
  2751. of nkObjDownConv: gen(p, n[0], r)
  2752. of nkObjUpConv: upConv(p, n, r)
  2753. of nkCast: genCast(p, n, r)
  2754. of nkChckRangeF: genRangeChck(p, n, r, "chckRangeF")
  2755. of nkChckRange64: genRangeChck(p, n, r, "chckRange64")
  2756. of nkChckRange: genRangeChck(p, n, r, "chckRange")
  2757. of nkStringToCString: convStrToCStr(p, n, r)
  2758. of nkCStringToString: convCStrToStr(p, n, r)
  2759. of nkEmpty: discard
  2760. of nkLambdaKinds:
  2761. let s = n[namePos].sym
  2762. discard mangleName(p.module, s)
  2763. r.res = s.loc.r
  2764. if lfNoDecl in s.loc.flags or s.magic notin generatedMagics: discard
  2765. elif not p.g.generatedSyms.containsOrIncl(s.id):
  2766. p.locals.add(genProc(p, s))
  2767. of nkType: r.res = genTypeInfo(p, n.typ)
  2768. of nkStmtList, nkStmtListExpr:
  2769. # this shows the distinction is nice for backends and should be kept
  2770. # in the frontend
  2771. let isExpr = not isEmptyType(n.typ)
  2772. for i in 0..<n.len - isExpr.ord:
  2773. genStmt(p, n[i])
  2774. if isExpr:
  2775. gen(p, lastSon(n), r)
  2776. of nkBlockStmt, nkBlockExpr: genBlock(p, n, r)
  2777. of nkIfStmt, nkIfExpr: genIf(p, n, r)
  2778. of nkWhen:
  2779. # This is "when nimvm" node
  2780. gen(p, n[1][0], r)
  2781. of nkWhileStmt: genWhileStmt(p, n)
  2782. of nkVarSection, nkLetSection: genVarStmt(p, n)
  2783. of nkConstSection: discard
  2784. of nkForStmt, nkParForStmt:
  2785. internalError(p.config, n.info, "for statement not eliminated")
  2786. of nkCaseStmt: genCaseJS(p, n, r)
  2787. of nkReturnStmt: genReturnStmt(p, n)
  2788. of nkBreakStmt: genBreakStmt(p, n)
  2789. of nkAsgn: genAsgn(p, n)
  2790. of nkFastAsgn, nkSinkAsgn: genFastAsgn(p, n)
  2791. of nkDiscardStmt:
  2792. if n[0].kind != nkEmpty:
  2793. genLineDir(p, n)
  2794. gen(p, n[0], r)
  2795. r.res = "var _ = " & r.res
  2796. of nkAsmStmt: genAsmOrEmitStmt(p, n)
  2797. of nkTryStmt, nkHiddenTryStmt: genTry(p, n, r)
  2798. of nkRaiseStmt: genRaiseStmt(p, n)
  2799. of nkTypeSection, nkCommentStmt, nkIncludeStmt,
  2800. nkImportStmt, nkImportExceptStmt, nkExportStmt, nkExportExceptStmt,
  2801. nkFromStmt, nkTemplateDef, nkMacroDef, nkStaticStmt,
  2802. nkMixinStmt, nkBindStmt: discard
  2803. of nkIteratorDef:
  2804. if n[0].sym.typ.callConv == TCallingConvention.ccClosure:
  2805. globalError(p.config, n.info, "Closure iterators are not supported by JS backend!")
  2806. of nkPragma: genPragma(p, n)
  2807. of nkProcDef, nkFuncDef, nkMethodDef, nkConverterDef:
  2808. var s = n[namePos].sym
  2809. if {sfExportc, sfCompilerProc} * s.flags == {sfExportc}:
  2810. genSym(p, n[namePos], r)
  2811. r.res = ""
  2812. of nkGotoState, nkState:
  2813. globalError(p.config, n.info, "First class iterators not implemented")
  2814. of nkPragmaBlock: gen(p, n.lastSon, r)
  2815. of nkComesFrom:
  2816. discard "XXX to implement for better stack traces"
  2817. else: internalError(p.config, n.info, "gen: unknown node type: " & $n.kind)
  2818. proc newModule(g: ModuleGraph; module: PSym): BModule =
  2819. ## Create a new JS backend module node.
  2820. if g.backend == nil:
  2821. g.backend = newGlobals()
  2822. result = BModule(module: module, sigConflicts: initCountTable[SigHash](),
  2823. graph: g, config: g.config
  2824. )
  2825. if sfSystemModule in module.flags:
  2826. PGlobals(g.backend).inSystem = true
  2827. proc genHeader(): Rope =
  2828. ## Generate the JS header.
  2829. result = rope("""/* Generated by the Nim Compiler v$1 */
  2830. var framePtr = null;
  2831. var excHandler = 0;
  2832. var lastJSError = null;
  2833. """.unindent.format(VersionAsString))
  2834. proc addHcrInitGuards(p: PProc, n: PNode,
  2835. moduleLoadedVar: Rope, inInitGuard: var bool) =
  2836. if n.kind == nkStmtList:
  2837. for child in n:
  2838. addHcrInitGuards(p, child, moduleLoadedVar, inInitGuard)
  2839. else:
  2840. let stmtShouldExecute = n.kind in {
  2841. nkProcDef, nkFuncDef, nkMethodDef,nkConverterDef,
  2842. nkVarSection, nkLetSection} or nfExecuteOnReload in n.flags
  2843. if inInitGuard:
  2844. if stmtShouldExecute:
  2845. dec p.extraIndent
  2846. line(p, "}\L")
  2847. inInitGuard = false
  2848. else:
  2849. if not stmtShouldExecute:
  2850. lineF(p, "if ($1 == undefined) {$n", [moduleLoadedVar])
  2851. inc p.extraIndent
  2852. inInitGuard = true
  2853. genStmt(p, n)
  2854. proc genModule(p: PProc, n: PNode) =
  2855. ## Generate the JS module code.
  2856. ## Called for each top level node in a Nim module.
  2857. if optStackTrace in p.options:
  2858. p.body.add(frameCreate(p,
  2859. makeJSString("module " & p.module.module.name.s),
  2860. makeJSString(toFilenameOption(p.config, p.module.module.info.fileIndex, foStacktrace))))
  2861. var transformedN = transformStmt(p.module.graph, p.module.idgen, p.module.module, n)
  2862. if sfInjectDestructors in p.module.module.flags:
  2863. transformedN = injectDestructorCalls(p.module.graph, p.module.idgen, p.module.module, transformedN)
  2864. if p.config.hcrOn and n.kind == nkStmtList:
  2865. let moduleSym = p.module.module
  2866. var moduleLoadedVar = rope(moduleSym.name.s) & "_loaded" &
  2867. idOrSig(moduleSym, moduleSym.name.s, p.module.sigConflicts, p.config)
  2868. lineF(p, "var $1;$n", [moduleLoadedVar])
  2869. var inGuardedBlock = false
  2870. addHcrInitGuards(p, transformedN, moduleLoadedVar, inGuardedBlock)
  2871. if inGuardedBlock:
  2872. dec p.extraIndent
  2873. line(p, "}\L")
  2874. lineF(p, "$1 = true;$n", [moduleLoadedVar])
  2875. else:
  2876. genStmt(p, transformedN)
  2877. if optStackTrace in p.options:
  2878. p.body.add(frameDestroy(p))
  2879. proc processJSCodeGen*(b: PPassContext, n: PNode): PNode =
  2880. ## Generate JS code for a node.
  2881. result = n
  2882. let m = BModule(b)
  2883. if pipelineutils.skipCodegen(m.config, n): return n
  2884. if m.module == nil: internalError(m.config, n.info, "myProcess")
  2885. let globals = PGlobals(m.graph.backend)
  2886. var p = newInitProc(globals, m)
  2887. m.initProc = p
  2888. p.unique = globals.unique
  2889. genModule(p, n)
  2890. p.g.code.add(p.locals)
  2891. p.g.code.add(p.body)
  2892. proc wholeCode(graph: ModuleGraph; m: BModule): Rope =
  2893. ## Combine source code from all nodes.
  2894. let globals = PGlobals(graph.backend)
  2895. for prc in globals.forwarded:
  2896. if not globals.generatedSyms.containsOrIncl(prc.id):
  2897. var p = newInitProc(globals, m)
  2898. attachProc(p, prc)
  2899. var disp = generateMethodDispatchers(graph, m.idgen)
  2900. for i in 0..<disp.len:
  2901. let prc = disp[i].sym
  2902. if not globals.generatedSyms.containsOrIncl(prc.id):
  2903. var p = newInitProc(globals, m)
  2904. attachProc(p, prc)
  2905. result = globals.typeInfo & globals.constants & globals.code
  2906. proc getClassName(t: PType): Rope =
  2907. var s = t.sym
  2908. if s.isNil or sfAnon in s.flags:
  2909. s = skipTypes(t, abstractPtrs).sym
  2910. if s.isNil or sfAnon in s.flags:
  2911. doAssert(false, "cannot retrieve class name")
  2912. if s.loc.r != "": result = s.loc.r
  2913. else: result = rope(s.name.s)
  2914. proc finalJSCodeGen*(graph: ModuleGraph; b: PPassContext, n: PNode): PNode =
  2915. ## Finalize JS code generation of a Nim module.
  2916. ## Param `n` may contain nodes returned from the last module close call.
  2917. var m = BModule(b)
  2918. if sfMainModule in m.module.flags:
  2919. # Add global destructors to the module.
  2920. # This must come before the last call to `myProcess`.
  2921. for i in countdown(high(graph.globalDestructors), 0):
  2922. n.add graph.globalDestructors[i]
  2923. # Process any nodes left over from the last call to `myClose`.
  2924. result = processJSCodeGen(b, n)
  2925. # Some codegen is different (such as no stacktraces; see `initProcOptions`)
  2926. # when `std/system` is being processed.
  2927. if sfSystemModule in m.module.flags:
  2928. PGlobals(graph.backend).inSystem = false
  2929. # Check if codegen should continue before any files are generated.
  2930. # It may bail early is if too many errors have been raised.
  2931. if pipelineutils.skipCodegen(m.config, n): return n
  2932. # Nim modules are compiled into a single JS file.
  2933. # If this is the main module, then this is the final call to `myClose`.
  2934. if sfMainModule in m.module.flags:
  2935. var code = genHeader() & wholeCode(graph, m)
  2936. let outFile = m.config.prepareToWriteOutput()
  2937. # Generate an optional source map.
  2938. if optSourcemap in m.config.globalOptions:
  2939. var map: SourceMap
  2940. map = genSourceMap($code, outFile.string)
  2941. code &= "\n//# sourceMappingURL=$#.map" % [outFile.string]
  2942. writeFile(outFile.string & ".map", $(%map))
  2943. # Check if the generated JS code matches the output file, or else
  2944. # write it to the file.
  2945. if not equalsFile(code, outFile):
  2946. if not writeRope(code, outFile):
  2947. rawMessage(m.config, errCannotOpenFile, outFile.string)
  2948. proc setupJSgen*(graph: ModuleGraph; s: PSym; idgen: IdGenerator): PPassContext =
  2949. result = newModule(graph, s)
  2950. result.idgen = idgen