jsgen.nim 107 KB

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