12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528 |
- #
- #
- # The Nim Compiler
- # (c) Copyright 2015 Andreas Rumpf
- #
- # See the file "copying.txt", included in this
- # distribution, for details about the copyright.
- #
- # This is the JavaScript code generator.
- discard """
- The JS code generator contains only 2 tricks:
- Trick 1
- -------
- Some locations (for example 'var int') require "fat pointers" (``etyBaseIndex``)
- which are pairs (array, index). The derefence operation is then 'array[index]'.
- Check ``mapType`` for the details.
- Trick 2
- -------
- It is preferable to generate '||' and '&&' if possible since that is more
- idiomatic and hence should be friendlier for the JS JIT implementation. However
- code like ``foo and (let bar = baz())`` cannot be translated this way. Instead
- the expressions need to be transformed into statements. ``isSimpleExpr``
- implements the required case distinction.
- """
- import
- ast, astalgo, strutils, hashes, trees, platform, magicsys, extccomp, options,
- nversion, nimsets, msgs, std / sha1, bitsets, idents, types, os, tables,
- times, ropes, math, passes, ccgutils, wordrecg, renderer,
- intsets, cgmeth, lowerings, sighashes, lineinfos, rodutils, pathutils, transf
- from modulegraphs import ModuleGraph, PPassContext
- type
- TJSGen = object of PPassContext
- module: PSym
- graph: ModuleGraph
- config: ConfigRef
- sigConflicts: CountTable[SigHash]
- BModule = ref TJSGen
- TJSTypeKind = enum # necessary JS "types"
- etyNone, # no type
- etyNull, # null type
- etyProc, # proc type
- etyBool, # bool type
- etySeq, # Nim seq or string type
- etyInt, # JavaScript's int
- etyFloat, # JavaScript's float
- etyString, # JavaScript's string
- etyObject, # JavaScript's reference to an object
- etyBaseIndex # base + index needed
- TResKind = enum
- resNone, # not set
- resExpr, # is some complex expression
- resVal, # is a temporary/value/l-value
- resCallee # expression is callee
- TCompRes = object
- kind: TResKind
- typ: TJSTypeKind
- res: Rope # result part; index if this is an
- # (address, index)-tuple
- address: Rope # address of an (address, index)-tuple
- tmpLoc: Rope # tmp var which stores the (address, index)
- # pair to prevent multiple evals.
- # the tmp is initialized upon evaling the
- # address.
- # might be nil.
- # (see `maybeMakeTemp`)
- TBlock = object
- id: int # the ID of the label; positive means that it
- # has been used (i.e. the label should be emitted)
- isLoop: bool # whether it's a 'block' or 'while'
- PGlobals = ref object of RootObj
- typeInfo, constants, code: Rope
- forwarded: seq[PSym]
- generatedSyms: IntSet
- typeInfoGenerated: IntSet
- classes: seq[(PType, Rope)]
- unique: int # for temp identifier generation
- PProc = ref TProc
- TProc = object
- procDef: PNode
- prc: PSym
- globals, locals, body: Rope
- options: TOptions
- module: BModule
- g: PGlobals
- beforeRetNeeded: bool
- unique: int # for temp identifier generation
- blocks: seq[TBlock]
- extraIndent: int
- up: PProc # up the call chain; required for closure support
- declaredGlobals: IntSet
- template config*(p: PProc): ConfigRef = p.module.config
- proc indentLine(p: PProc, r: Rope): Rope =
- result = r
- var p = p
- while true:
- for i in countup(0, p.blocks.len - 1 + p.extraIndent):
- prepend(result, "\t".rope)
- if p.up == nil or p.up.prc != p.prc.owner:
- break
- p = p.up
- template line(p: PProc, added: string) =
- add(p.body, indentLine(p, rope(added)))
- template line(p: PProc, added: Rope) =
- add(p.body, indentLine(p, added))
- template lineF(p: PProc, frmt: FormatStr, args: varargs[Rope]) =
- add(p.body, indentLine(p, ropes.`%`(frmt, args)))
- template nested(p, body) =
- inc p.extraIndent
- body
- dec p.extraIndent
- proc newGlobals(): PGlobals =
- new(result)
- result.forwarded = @[]
- result.generatedSyms = initIntSet()
- result.typeInfoGenerated = initIntSet()
- result.classes = @[]
- proc initCompRes(r: var TCompRes) =
- r.address = nil
- r.res = nil
- r.tmpLoc = nil
- r.typ = etyNone
- r.kind = resNone
- proc rdLoc(a: TCompRes): Rope {.inline.} =
- if a.typ != etyBaseIndex:
- result = a.res
- else:
- result = "$1[$2]" % [a.address, a.res]
- proc newProc(globals: PGlobals, module: BModule, procDef: PNode,
- options: TOptions): PProc =
- result = PProc(
- blocks: @[],
- options: options,
- module: module,
- procDef: procDef,
- g: globals,
- extraIndent: int(procDef != nil))
- if procDef != nil: result.prc = procDef.sons[namePos].sym
- proc declareGlobal(p: PProc; id: int; r: Rope) =
- if p.prc != nil and not p.declaredGlobals.containsOrIncl(id):
- p.locals.addf("global $1;$n", [r])
- const
- MappedToObject = {tyObject, tyArray, tyTuple, tyOpenArray,
- tySet, tyVarargs}
- proc mapType(typ: PType): TJSTypeKind =
- let t = skipTypes(typ, abstractInst)
- case t.kind
- of tyVar, tyRef, tyPtr, tyLent:
- if skipTypes(t.lastSon, abstractInst).kind in MappedToObject:
- result = etyObject
- else:
- result = etyBaseIndex
- of tyPointer:
- # treat a tyPointer like a typed pointer to an array of bytes
- result = etyBaseIndex
- of tyRange, tyDistinct, tyOrdinal, tyProxy:
- result = mapType(t.sons[0])
- of tyInt..tyInt64, tyUInt..tyUInt64, tyEnum, tyChar: result = etyInt
- of tyBool: result = etyBool
- of tyFloat..tyFloat128: result = etyFloat
- of tySet: result = etyObject # map a set to a table
- of tyString, tySequence, tyOpt: result = etySeq
- of tyObject, tyArray, tyTuple, tyOpenArray, tyVarargs, tyUncheckedArray:
- result = etyObject
- of tyNil: result = etyNull
- of tyGenericParam, tyGenericBody, tyGenericInvocation,
- tyNone, tyFromExpr, tyForward, tyEmpty,
- tyExpr, tyStmt, tyTypeDesc, tyBuiltInTypeClass, tyCompositeTypeClass,
- tyAnd, tyOr, tyNot, tyAnything, tyVoid:
- result = etyNone
- of tyGenericInst, tyInferred, tyAlias, tyUserTypeClass, tyUserTypeClassInst,
- tySink:
- result = mapType(typ.lastSon)
- of tyStatic:
- if t.n != nil: result = mapType(lastSon t)
- else: result = etyNone
- of tyProc: result = etyProc
- of tyCString: result = etyString
- of tyOptAsRef: doAssert(false, "mapType")
- proc mapType(p: PProc; typ: PType): TJSTypeKind =
- result = mapType(typ)
- proc mangleName(m: BModule, s: PSym): Rope =
- proc validJsName(name: string): bool =
- result = true
- const reservedWords = ["abstract", "await", "boolean", "break", "byte",
- "case", "catch", "char", "class", "const", "continue", "debugger",
- "default", "delete", "do", "double", "else", "enum", "export", "extends",
- "false", "final", "finally", "float", "for", "function", "goto", "if",
- "implements", "import", "in", "instanceof", "int", "interface", "let",
- "long", "native", "new", "null", "package", "private", "protected",
- "public", "return", "short", "static", "super", "switch", "synchronized",
- "this", "throw", "throws", "transient", "true", "try", "typeof", "var",
- "void", "volatile", "while", "with", "yield"]
- case name
- of reservedWords:
- return false
- else:
- discard
- if name[0] in {'0'..'9'}: return false
- for chr in name:
- if chr notin {'A'..'Z','a'..'z','_','$','0'..'9'}:
- return false
- result = s.loc.r
- if result == nil:
- if s.kind == skField and s.name.s.validJsName:
- result = rope(s.name.s)
- elif s.kind == skTemp:
- result = rope(mangle(s.name.s))
- else:
- var x = newStringOfCap(s.name.s.len)
- var i = 0
- while i < s.name.s.len:
- let c = s.name.s[i]
- case c
- of 'A'..'Z':
- if i > 0 and s.name.s[i-1] in {'a'..'z'}:
- x.add '_'
- x.add(chr(c.ord - 'A'.ord + 'a'.ord))
- of 'a'..'z', '_', '0'..'9':
- x.add c
- else:
- x.add("HEX" & toHex(ord(c), 2))
- inc i
- result = rope(x)
- # From ES5 on reserved words can be used as object field names
- if s.kind != skField:
- if optHotCodeReloading in m.config.options:
- # When hot reloading is enabled, we must ensure that the names
- # of functions and types will be preserved across rebuilds:
- add(result, idOrSig(s, m.module.name.s, m.sigConflicts))
- else:
- add(result, "_")
- add(result, rope(s.id))
- s.loc.r = result
- proc escapeJSString(s: string): string =
- result = newStringOfCap(s.len + s.len shr 2)
- result.add("\"")
- for c in items(s):
- case c
- of '\l': result.add("\\n")
- of '\r': result.add("\\r")
- of '\t': result.add("\\t")
- of '\b': result.add("\\b")
- of '\a': result.add("\\a")
- of '\e': result.add("\\e")
- of '\v': result.add("\\v")
- of '\\': result.add("\\\\")
- of '\"': result.add("\\\"")
- else: add(result, c)
- result.add("\"")
- proc makeJSString(s: string, escapeNonAscii = true): Rope =
- if escapeNonAscii:
- result = strutils.escape(s).rope
- else:
- result = escapeJSString(s).rope
- include jstypes
- proc gen(p: PProc, n: PNode, r: var TCompRes)
- proc genStmt(p: PProc, n: PNode)
- proc genProc(oldProc: PProc, prc: PSym): Rope
- proc genConstant(p: PProc, c: PSym)
- proc useMagic(p: PProc, name: string) =
- if name.len == 0: return
- var s = magicsys.getCompilerProc(p.module.graph, name)
- if s != nil:
- internalAssert p.config, s.kind in {skProc, skFunc, skMethod, skConverter}
- if not p.g.generatedSyms.containsOrIncl(s.id):
- let code = genProc(p, s)
- add(p.g.constants, code)
- else:
- if p.prc != nil:
- globalError(p.config, p.prc.info, "system module needs: " & name)
- else:
- rawMessage(p.config, errGenerated, "system module needs: " & name)
- proc isSimpleExpr(p: PProc; n: PNode): bool =
- # calls all the way down --> can stay expression based
- if n.kind in nkCallKinds+{nkBracketExpr, nkDotExpr, nkPar, nkTupleConstr} or
- (n.kind in {nkObjConstr, nkBracket, nkCurly}):
- for c in n:
- if not p.isSimpleExpr(c): return false
- result = true
- elif n.isAtom:
- result = true
- proc getTemp(p: PProc, defineInLocals: bool = true): Rope =
- inc(p.unique)
- result = "Tmp$1" % [rope(p.unique)]
- if defineInLocals:
- add(p.locals, p.indentLine("var $1;$n" % [result]))
- proc genAnd(p: PProc, a, b: PNode, r: var TCompRes) =
- assert r.kind == resNone
- var x, y: TCompRes
- if p.isSimpleExpr(a) and p.isSimpleExpr(b):
- gen(p, a, x)
- gen(p, b, y)
- r.kind = resExpr
- r.res = "($1 && $2)" % [x.rdLoc, y.rdLoc]
- else:
- r.res = p.getTemp
- r.kind = resVal
- # while a and b:
- # -->
- # while true:
- # aa
- # if not a: tmp = false
- # else:
- # bb
- # tmp = b
- # tmp
- gen(p, a, x)
- lineF(p, "if (!$1) $2 = false; else {", [x.rdLoc, r.rdLoc])
- p.nested:
- gen(p, b, y)
- lineF(p, "$2 = $1;", [y.rdLoc, r.rdLoc])
- line(p, "}")
- proc genOr(p: PProc, a, b: PNode, r: var TCompRes) =
- assert r.kind == resNone
- var x, y: TCompRes
- if p.isSimpleExpr(a) and p.isSimpleExpr(b):
- gen(p, a, x)
- gen(p, b, y)
- r.kind = resExpr
- r.res = "($1 || $2)" % [x.rdLoc, y.rdLoc]
- else:
- r.res = p.getTemp
- r.kind = resVal
- gen(p, a, x)
- lineF(p, "if ($1) $2 = true; else {", [x.rdLoc, r.rdLoc])
- p.nested:
- gen(p, b, y)
- lineF(p, "$2 = $1;", [y.rdLoc, r.rdLoc])
- line(p, "}")
- type
- TMagicFrmt = array[0..3, string]
- TMagicOps = array[mAddI..mStrToStr, TMagicFrmt]
- const # magic checked op; magic unchecked op; checked op; unchecked op
- jsOps: TMagicOps = [
- ["addInt", "", "addInt($1, $2)", "($1 + $2)"], # AddI
- ["subInt", "", "subInt($1, $2)", "($1 - $2)"], # SubI
- ["mulInt", "", "mulInt($1, $2)", "($1 * $2)"], # MulI
- ["divInt", "", "divInt($1, $2)", "Math.trunc($1 / $2)"], # DivI
- ["modInt", "", "modInt($1, $2)", "Math.trunc($1 % $2)"], # ModI
- ["addInt", "", "addInt($1, $2)", "($1 + $2)"], # Succ
- ["subInt", "", "subInt($1, $2)", "($1 - $2)"], # Pred
- ["", "", "($1 + $2)", "($1 + $2)"], # AddF64
- ["", "", "($1 - $2)", "($1 - $2)"], # SubF64
- ["", "", "($1 * $2)", "($1 * $2)"], # MulF64
- ["", "", "($1 / $2)", "($1 / $2)"], # DivF64
- ["", "", "", ""], # ShrI
- ["", "", "($1 << $2)", "($1 << $2)"], # ShlI
- ["", "", "($1 >> $2)", "($1 >> $2)"], # AshrI
- ["", "", "($1 & $2)", "($1 & $2)"], # BitandI
- ["", "", "($1 | $2)", "($1 | $2)"], # BitorI
- ["", "", "($1 ^ $2)", "($1 ^ $2)"], # BitxorI
- ["nimMin", "nimMin", "nimMin($1, $2)", "nimMin($1, $2)"], # MinI
- ["nimMax", "nimMax", "nimMax($1, $2)", "nimMax($1, $2)"], # MaxI
- ["nimMin", "nimMin", "nimMin($1, $2)", "nimMin($1, $2)"], # MinF64
- ["nimMax", "nimMax", "nimMax($1, $2)", "nimMax($1, $2)"], # MaxF64
- ["", "", "", ""], # addU
- ["", "", "", ""], # subU
- ["", "", "", ""], # mulU
- ["", "", "", ""], # divU
- ["", "", "($1 % $2)", "($1 % $2)"], # modU
- ["", "", "($1 == $2)", "($1 == $2)"], # EqI
- ["", "", "($1 <= $2)", "($1 <= $2)"], # LeI
- ["", "", "($1 < $2)", "($1 < $2)"], # LtI
- ["", "", "($1 == $2)", "($1 == $2)"], # EqF64
- ["", "", "($1 <= $2)", "($1 <= $2)"], # LeF64
- ["", "", "($1 < $2)", "($1 < $2)"], # LtF64
- ["", "", "($1 <= $2)", "($1 <= $2)"], # leU
- ["", "", "($1 < $2)", "($1 < $2)"], # ltU
- ["", "", "($1 <= $2)", "($1 <= $2)"], # leU64
- ["", "", "($1 < $2)", "($1 < $2)"], # ltU64
- ["", "", "($1 == $2)", "($1 == $2)"], # EqEnum
- ["", "", "($1 <= $2)", "($1 <= $2)"], # LeEnum
- ["", "", "($1 < $2)", "($1 < $2)"], # LtEnum
- ["", "", "($1 == $2)", "($1 == $2)"], # EqCh
- ["", "", "($1 <= $2)", "($1 <= $2)"], # LeCh
- ["", "", "($1 < $2)", "($1 < $2)"], # LtCh
- ["", "", "($1 == $2)", "($1 == $2)"], # EqB
- ["", "", "($1 <= $2)", "($1 <= $2)"], # LeB
- ["", "", "($1 < $2)", "($1 < $2)"], # LtB
- ["", "", "($1 == $2)", "($1 == $2)"], # EqRef
- ["", "", "($1 == $2)", "($1 == $2)"], # EqUntracedRef
- ["", "", "($1 <= $2)", "($1 <= $2)"], # LePtr
- ["", "", "($1 < $2)", "($1 < $2)"], # LtPtr
- ["", "", "($1 != $2)", "($1 != $2)"], # Xor
- ["", "", "($1 == $2)", "($1 == $2)"], # EqCString
- ["", "", "($1 == $2)", "($1 == $2)"], # EqProc
- ["negInt", "", "negInt($1)", "-($1)"], # UnaryMinusI
- ["negInt64", "", "negInt64($1)", "-($1)"], # UnaryMinusI64
- ["absInt", "", "absInt($1)", "Math.abs($1)"], # AbsI
- ["", "", "!($1)", "!($1)"], # Not
- ["", "", "+($1)", "+($1)"], # UnaryPlusI
- ["", "", "~($1)", "~($1)"], # BitnotI
- ["", "", "+($1)", "+($1)"], # UnaryPlusF64
- ["", "", "-($1)", "-($1)"], # UnaryMinusF64
- ["", "", "Math.abs($1)", "Math.abs($1)"], # AbsF64
- ["Ze8ToI", "Ze8ToI", "Ze8ToI($1)", "Ze8ToI($1)"], # mZe8ToI
- ["Ze8ToI64", "Ze8ToI64", "Ze8ToI64($1)", "Ze8ToI64($1)"], # mZe8ToI64
- ["Ze16ToI", "Ze16ToI", "Ze16ToI($1)", "Ze16ToI($1)"], # mZe16ToI
- ["Ze16ToI64", "Ze16ToI64", "Ze16ToI64($1)", "Ze16ToI64($1)"], # mZe16ToI64
- ["Ze32ToI64", "Ze32ToI64", "Ze32ToI64($1)", "Ze32ToI64($1)"], # mZe32ToI64
- ["ZeIToI64", "ZeIToI64", "ZeIToI64($1)", "ZeIToI64($1)"], # mZeIToI64
- ["toU8", "toU8", "toU8($1)", "toU8($1)"], # toU8
- ["toU16", "toU16", "toU16($1)", "toU16($1)"], # toU16
- ["toU32", "toU32", "toU32($1)", "toU32($1)"], # toU32
- ["", "", "$1", "$1"], # ToFloat
- ["", "", "$1", "$1"], # ToBiggestFloat
- ["", "", "Math.trunc($1)", "Math.trunc($1)"], # ToInt
- ["", "", "Math.trunc($1)", "Math.trunc($1)"], # ToBiggestInt
- ["nimCharToStr", "nimCharToStr", "nimCharToStr($1)", "nimCharToStr($1)"],
- ["nimBoolToStr", "nimBoolToStr", "nimBoolToStr($1)", "nimBoolToStr($1)"],
- ["cstrToNimstr", "cstrToNimstr", "cstrToNimstr(($1)+\"\")", "cstrToNimstr(($1)+\"\")"],
- ["cstrToNimstr", "cstrToNimstr", "cstrToNimstr(($1)+\"\")", "cstrToNimstr(($1)+\"\")"],
- ["cstrToNimstr", "cstrToNimstr", "cstrToNimstr(($1)+\"\")", "cstrToNimstr(($1)+\"\")"],
- ["cstrToNimstr", "cstrToNimstr", "cstrToNimstr($1)", "cstrToNimstr($1)"],
- ["", "", "$1", "$1"]]
- proc needsTemp(p: PProc; n: PNode): bool =
- # check if n contains a call to determine
- # if a temp should be made to prevent multiple evals
- if n.kind in nkCallKinds + {nkTupleConstr, nkObjConstr, nkBracket, nkCurly}:
- return true
- for c in n:
- if needsTemp(p, c):
- return true
- proc maybeMakeTemp(p: PProc, n: PNode; x: TCompRes): tuple[a, tmp: Rope] =
- var
- a = x.rdLoc
- b = a
- if needsTemp(p, n):
- # if we have tmp just use it
- if x.tmpLoc != nil and (mapType(n.typ) == etyBaseIndex or n.kind in {nkHiddenDeref, nkDerefExpr}):
- b = "$1[0][$1[1]]" % [x.tmpLoc]
- (a: a, tmp: b)
- else:
- let tmp = p.getTemp
- b = tmp
- a = "($1 = $2, $1)" % [tmp, a]
- (a: a, tmp: b)
- else:
- (a: a, tmp: b)
- proc binaryExpr(p: PProc, n: PNode, r: var TCompRes, magic, frmt: string) =
- # $1 and $2 in the `frmt` string bind to lhs and rhs of the expr,
- # if $3 or $4 are present they will be substituted with temps for
- # lhs and rhs respectively
- var x, y: TCompRes
- useMagic(p, magic)
- gen(p, n.sons[1], x)
- gen(p, n.sons[2], y)
- var
- a, tmp = x.rdLoc
- b, tmp2 = y.rdLoc
- if "$3" in frmt: (a, tmp) = maybeMakeTemp(p, n[1], x)
- if "$4" in frmt: (a, tmp) = maybeMakeTemp(p, n[1], x)
- r.res = frmt % [a, b, tmp, tmp2]
- r.kind = resExpr
- proc unsignedTrimmerJS(size: BiggestInt): Rope =
- case size
- of 1: rope"& 0xff"
- of 2: rope"& 0xffff"
- of 4: rope">>> 0"
- else: rope""
- template unsignedTrimmer(size: BiggestInt): Rope =
- size.unsignedTrimmerJS
- proc binaryUintExpr(p: PProc, n: PNode, r: var TCompRes, op: string,
- reassign = false) =
- var x, y: TCompRes
- gen(p, n.sons[1], x)
- gen(p, n.sons[2], y)
- let trimmer = unsignedTrimmer(n[1].typ.skipTypes(abstractRange).size)
- if reassign:
- let (a, tmp) = maybeMakeTemp(p, n[1], x)
- r.res = "$1 = (($5 $2 $3) $4)" % [a, rope op, y.rdLoc, trimmer, tmp]
- else:
- r.res = "(($1 $2 $3) $4)" % [x.rdLoc, rope op, y.rdLoc, trimmer]
- proc ternaryExpr(p: PProc, n: PNode, r: var TCompRes, magic, frmt: string) =
- var x, y, z: TCompRes
- useMagic(p, magic)
- gen(p, n.sons[1], x)
- gen(p, n.sons[2], y)
- gen(p, n.sons[3], z)
- r.res = frmt % [x.rdLoc, y.rdLoc, z.rdLoc]
- r.kind = resExpr
- proc unaryExpr(p: PProc, n: PNode, r: var TCompRes, magic, frmt: string) =
- # $1 binds to n[1], if $2 is present it will be substituted to a tmp of $1
- useMagic(p, magic)
- gen(p, n.sons[1], r)
- var a, tmp = r.rdLoc
- if "$2" in frmt: (a, tmp) = maybeMakeTemp(p, n[1], r)
- r.res = frmt % [a, tmp]
- r.kind = resExpr
- proc arithAux(p: PProc, n: PNode, r: var TCompRes, op: TMagic) =
- var
- x, y: TCompRes
- let i = ord(optOverflowCheck notin p.options)
- useMagic(p, jsOps[op][i])
- if sonsLen(n) > 2:
- gen(p, n.sons[1], x)
- gen(p, n.sons[2], y)
- r.res = jsOps[op][i + 2] % [x.rdLoc, y.rdLoc]
- else:
- gen(p, n.sons[1], r)
- r.res = jsOps[op][i + 2] % [r.rdLoc]
- proc arith(p: PProc, n: PNode, r: var TCompRes, op: TMagic) =
- case op
- of mAddU: binaryUintExpr(p, n, r, "+")
- of mSubU: binaryUintExpr(p, n, r, "-")
- of mMulU: binaryUintExpr(p, n, r, "*")
- of mDivU: binaryUintExpr(p, n, r, "/")
- of mDivI:
- arithAux(p, n, r, op)
- of mModI:
- arithAux(p, n, r, op)
- of mShrI:
- var x, y: TCompRes
- gen(p, n.sons[1], x)
- gen(p, n.sons[2], y)
- let trimmer = unsignedTrimmer(n[1].typ.skipTypes(abstractRange).size)
- r.res = "(($1 $2) >>> $3)" % [x.rdLoc, trimmer, y.rdLoc]
- of mCharToStr, mBoolToStr, mIntToStr, mInt64ToStr, mFloatToStr,
- mCStrToStr, mStrToStr, mEnumToStr:
- arithAux(p, n, r, op)
- of mEqRef, mEqUntracedRef:
- if mapType(n[1].typ) != etyBaseIndex:
- arithAux(p, n, r, op)
- else:
- var x, y: TCompRes
- gen(p, n[1], x)
- gen(p, n[2], y)
- r.res = "($# == $# && $# == $#)" % [x.address, y.address, x.res, y.res]
- else:
- arithAux(p, n, r, op)
- r.kind = resExpr
- proc hasFrameInfo(p: PProc): bool =
- ({optLineTrace, optStackTrace} * p.options == {optLineTrace, optStackTrace}) and
- ((p.prc == nil) or not (sfPure in p.prc.flags))
- proc genLineDir(p: PProc, n: PNode) =
- let line = toLinenumber(n.info)
- if optLineDir in p.options:
- lineF(p, "// line $2 \"$1\"$n",
- [rope(toFilename(p.config, n.info)), rope(line)])
- if {optStackTrace, optEndb} * p.options == {optStackTrace, optEndb} and
- ((p.prc == nil) or sfPure notin p.prc.flags):
- useMagic(p, "endb")
- lineF(p, "endb($1);$n", [rope(line)])
- elif hasFrameInfo(p):
- lineF(p, "F.line = $1;$n", [rope(line)])
- proc genWhileStmt(p: PProc, n: PNode) =
- var
- cond: TCompRes
- internalAssert p.config, isEmptyType(n.typ)
- genLineDir(p, n)
- inc(p.unique)
- var length = len(p.blocks)
- setLen(p.blocks, length + 1)
- p.blocks[length].id = -p.unique
- p.blocks[length].isLoop = true
- let labl = p.unique.rope
- lineF(p, "L$1: while (true) {$n", [labl])
- p.nested: gen(p, n.sons[0], cond)
- lineF(p, "if (!$1) break L$2;$n",
- [cond.res, labl])
- p.nested: genStmt(p, n.sons[1])
- lineF(p, "}$n", [labl])
- setLen(p.blocks, length)
- proc moveInto(p: PProc, src: var TCompRes, dest: TCompRes) =
- if src.kind != resNone:
- if dest.kind != resNone:
- lineF(p, "$1 = $2;$n", [dest.rdLoc, src.rdLoc])
- else:
- lineF(p, "$1;$n", [src.rdLoc])
- src.kind = resNone
- src.res = nil
- proc genTry(p: PProc, n: PNode, r: var TCompRes) =
- # code to generate:
- #
- # ++excHandler;
- # var tmpFramePtr = framePtr;
- # try {
- # stmts;
- # --excHandler;
- # } catch (EXC) {
- # var prevJSError = lastJSError; lastJSError = EXC;
- # framePtr = tmpFramePtr;
- # --excHandler;
- # if (e.typ && e.typ == NTI433 || e.typ == NTI2321) {
- # stmts;
- # } else if (e.typ && e.typ == NTI32342) {
- # stmts;
- # } else {
- # stmts;
- # }
- # lastJSError = prevJSError;
- # } finally {
- # framePtr = tmpFramePtr;
- # stmts;
- # }
- genLineDir(p, n)
- if not isEmptyType(n.typ):
- r.kind = resVal
- r.res = getTemp(p)
- inc(p.unique)
- var i = 1
- var length = sonsLen(n)
- var catchBranchesExist = length > 1 and n.sons[i].kind == nkExceptBranch
- if catchBranchesExist:
- add(p.body, "++excHandler;\L")
- var tmpFramePtr = rope"F"
- if optStackTrace notin p.options:
- tmpFramePtr = p.getTemp(true)
- line(p, tmpFramePtr & " = framePtr;\L")
- lineF(p, "try {$n", [])
- var a: TCompRes
- gen(p, n.sons[0], a)
- moveInto(p, a, r)
- var generalCatchBranchExists = false
- if catchBranchesExist:
- addf(p.body, "--excHandler;$n} catch (EXC) {$n var prevJSError = lastJSError;$n" &
- " lastJSError = EXC;$n --excHandler;$n", [])
- line(p, "framePtr = $1;$n" % [tmpFramePtr])
- while i < length and n.sons[i].kind == nkExceptBranch:
- let blen = sonsLen(n.sons[i])
- if blen == 1:
- # general except section:
- generalCatchBranchExists = true
- if i > 1: lineF(p, "else {$n", [])
- gen(p, n.sons[i].sons[0], a)
- moveInto(p, a, r)
- if i > 1: lineF(p, "}$n", [])
- else:
- var orExpr: Rope = nil
- var excAlias: PNode = nil
- useMagic(p, "isObj")
- for j in countup(0, blen - 2):
- var throwObj: PNode
- let it = n.sons[i].sons[j]
- if it.isInfixAs():
- throwObj = it[1]
- excAlias = it[2]
- # If this is a ``except exc as sym`` branch there must be no following
- # nodes
- doAssert orExpr == nil
- elif it.kind == nkType:
- throwObj = it
- else:
- internalError(p.config, n.info, "genTryStmt")
- if orExpr != nil: add(orExpr, "||")
- # Generate the correct type checking code depending on whether this is a
- # NIM-native or a JS-native exception
- # if isJsObject(throwObj.typ):
- if isImportedException(throwObj.typ, p.config):
- addf(orExpr, "lastJSError instanceof $1",
- [throwObj.typ.sym.loc.r])
- else:
- addf(orExpr, "isObj(lastJSError.m_type, $1)",
- [genTypeInfo(p, throwObj.typ)])
- if i > 1: line(p, "else ")
- lineF(p, "if (lastJSError && ($1)) {$n", [orExpr])
- # If some branch requires a local alias introduce it here. This is needed
- # since JS cannot do ``catch x as y``.
- if excAlias != nil:
- excAlias.sym.loc.r = mangleName(p.module, excAlias.sym)
- lineF(p, "var $1 = lastJSError;$n", excAlias.sym.loc.r)
- gen(p, n.sons[i].sons[blen - 1], a)
- moveInto(p, a, r)
- lineF(p, "}$n", [])
- inc(i)
- if catchBranchesExist:
- if not generalCatchBranchExists:
- useMagic(p, "reraiseException")
- line(p, "else {\L")
- line(p, "\treraiseException();\L")
- line(p, "}\L")
- lineF(p, "lastJSError = prevJSError;$n")
- line(p, "} finally {\L")
- line(p, "framePtr = $1;$n" % [tmpFramePtr])
- if i < length and n.sons[i].kind == nkFinally:
- genStmt(p, n.sons[i].sons[0])
- line(p, "}\L")
- proc genRaiseStmt(p: PProc, n: PNode) =
- if n.sons[0].kind != nkEmpty:
- var a: TCompRes
- gen(p, n.sons[0], a)
- let typ = skipTypes(n.sons[0].typ, abstractPtrs)
- genLineDir(p, n)
- useMagic(p, "raiseException")
- lineF(p, "raiseException($1, $2);$n",
- [a.rdLoc, makeJSString(typ.sym.name.s)])
- else:
- genLineDir(p, n)
- useMagic(p, "reraiseException")
- line(p, "reraiseException();\L")
- proc genCaseJS(p: PProc, n: PNode, r: var TCompRes) =
- var
- cond, stmt: TCompRes
- genLineDir(p, n)
- gen(p, n.sons[0], cond)
- let stringSwitch = skipTypes(n.sons[0].typ, abstractVar).kind == tyString
- if stringSwitch:
- useMagic(p, "toJSStr")
- lineF(p, "switch (toJSStr($1)) {$n", [cond.rdLoc])
- else:
- lineF(p, "switch ($1) {$n", [cond.rdLoc])
- if not isEmptyType(n.typ):
- r.kind = resVal
- r.res = getTemp(p)
- for i in countup(1, sonsLen(n) - 1):
- let it = n.sons[i]
- case it.kind
- of nkOfBranch:
- for j in countup(0, sonsLen(it) - 2):
- let e = it.sons[j]
- if e.kind == nkRange:
- var v = copyNode(e.sons[0])
- while v.intVal <= e.sons[1].intVal:
- gen(p, v, cond)
- lineF(p, "case $1:$n", [cond.rdLoc])
- inc(v.intVal)
- else:
- if stringSwitch:
- case e.kind
- of nkStrLit..nkTripleStrLit: lineF(p, "case $1:$n",
- [makeJSString(e.strVal, false)])
- else: internalError(p.config, e.info, "jsgen.genCaseStmt: 2")
- else:
- gen(p, e, cond)
- lineF(p, "case $1:$n", [cond.rdLoc])
- p.nested:
- gen(p, lastSon(it), stmt)
- moveInto(p, stmt, r)
- lineF(p, "break;$n", [])
- of nkElse:
- lineF(p, "default: $n", [])
- p.nested:
- gen(p, it.sons[0], stmt)
- moveInto(p, stmt, r)
- lineF(p, "break;$n", [])
- else: internalError(p.config, it.info, "jsgen.genCaseStmt")
- lineF(p, "}$n", [])
- proc genBlock(p: PProc, n: PNode, r: var TCompRes) =
- inc(p.unique)
- let idx = len(p.blocks)
- if n.sons[0].kind != nkEmpty:
- # named block?
- if (n.sons[0].kind != nkSym): internalError(p.config, n.info, "genBlock")
- var sym = n.sons[0].sym
- sym.loc.k = locOther
- sym.position = idx+1
- let labl = p.unique
- lineF(p, "L$1: do {$n", [labl.rope])
- setLen(p.blocks, idx + 1)
- p.blocks[idx].id = - p.unique # negative because it isn't used yet
- gen(p, n.sons[1], r)
- setLen(p.blocks, idx)
- lineF(p, "} while(false);$n", [labl.rope])
- proc genBreakStmt(p: PProc, n: PNode) =
- var idx: int
- genLineDir(p, n)
- if n.sons[0].kind != nkEmpty:
- # named break?
- assert(n.sons[0].kind == nkSym)
- let sym = n.sons[0].sym
- assert(sym.loc.k == locOther)
- idx = sym.position-1
- else:
- # an unnamed 'break' can only break a loop after 'transf' pass:
- idx = len(p.blocks) - 1
- while idx >= 0 and not p.blocks[idx].isLoop: dec idx
- if idx < 0 or not p.blocks[idx].isLoop:
- internalError(p.config, n.info, "no loop to break")
- p.blocks[idx].id = abs(p.blocks[idx].id) # label is used
- lineF(p, "break L$1;$n", [rope(p.blocks[idx].id)])
- proc genAsmOrEmitStmt(p: PProc, n: PNode) =
- genLineDir(p, n)
- p.body.add p.indentLine(nil)
- for i in countup(0, sonsLen(n) - 1):
- let it = n[i]
- case it.kind
- of nkStrLit..nkTripleStrLit:
- p.body.add(it.strVal)
- of nkSym:
- let v = it.sym
- # for backwards compatibility we don't deref syms here :-(
- if false:
- discard
- else:
- var r: TCompRes
- gen(p, it, r)
- if it.typ.kind == tyPointer:
- # A fat pointer is disguised as an array
- r.res = r.address
- r.address = nil
- r.typ = etyNone
- elif r.typ == etyBaseIndex:
- # Deference first
- r.res = "$1[$2]" % [r.address, r.res]
- r.address = nil
- r.typ = etyNone
- p.body.add(r.rdLoc)
- else:
- var r: TCompRes
- gen(p, it, r)
- p.body.add(r.rdLoc)
- p.body.add "\L"
- proc genIf(p: PProc, n: PNode, r: var TCompRes) =
- var cond, stmt: TCompRes
- var toClose = 0
- if not isEmptyType(n.typ):
- r.kind = resVal
- r.res = getTemp(p)
- for i in countup(0, sonsLen(n) - 1):
- let it = n.sons[i]
- if sonsLen(it) != 1:
- if i > 0:
- lineF(p, "else {$n", [])
- inc(toClose)
- p.nested: gen(p, it.sons[0], cond)
- lineF(p, "if ($1) {$n", [cond.rdLoc])
- gen(p, it.sons[1], stmt)
- else:
- # else part:
- lineF(p, "else {$n", [])
- p.nested: gen(p, it.sons[0], stmt)
- moveInto(p, stmt, r)
- lineF(p, "}$n", [])
- line(p, repeat('}', toClose) & "\L")
- proc generateHeader(p: PProc, typ: PType): Rope =
- result = nil
- for i in countup(1, sonsLen(typ.n) - 1):
- assert(typ.n.sons[i].kind == nkSym)
- var param = typ.n.sons[i].sym
- if isCompileTimeOnly(param.typ): continue
- if result != nil: add(result, ", ")
- var name = mangleName(p.module, param)
- add(result, name)
- if mapType(param.typ) == etyBaseIndex:
- add(result, ", ")
- add(result, name)
- add(result, "_Idx")
- proc countJsParams(typ: PType): int =
- for i in countup(1, sonsLen(typ.n) - 1):
- assert(typ.n.sons[i].kind == nkSym)
- var param = typ.n.sons[i].sym
- if isCompileTimeOnly(param.typ): continue
- if mapType(param.typ) == etyBaseIndex:
- inc result, 2
- else:
- inc result
- const
- nodeKindsNeedNoCopy = {nkCharLit..nkInt64Lit, nkStrLit..nkTripleStrLit,
- nkFloatLit..nkFloat64Lit, nkCurly, nkPar, nkStringToCString,
- nkCStringToString, nkCall, nkPrefix, nkPostfix, nkInfix,
- nkCommand, nkHiddenCallConv, nkCallStrLit}
- proc needsNoCopy(p: PProc; y: PNode): bool =
- # if the node is a literal object constructor we have to recursively
- # check the expressions passed into it
- case y.kind
- of nkObjConstr:
- for arg in y.sons[1..^1]:
- if not needsNoCopy(p, arg[1]):
- return false
- of nkTupleConstr:
- for arg in y.sons:
- var arg = arg
- if arg.kind == nkExprColonExpr:
- arg = arg[1]
- if not needsNoCopy(p, arg):
- return false
- of nkBracket:
- for arg in y.sons:
- if not needsNoCopy(p, arg):
- return false
- of nodeKindsNeedNoCopy:
- return true
- else:
- return (mapType(y.typ) != etyBaseIndex and
- (skipTypes(y.typ, abstractInst).kind in
- {tyRef, tyPtr, tyLent, tyVar, tyCString, tyProc} + IntegralTypes))
- return true
- proc genAsgnAux(p: PProc, x, y: PNode, noCopyNeeded: bool) =
- var a, b: TCompRes
- var xtyp = mapType(p, x.typ)
- gen(p, x, a)
- genLineDir(p, y)
- gen(p, y, b)
- # we don't care if it's an etyBaseIndex (global) of a string, it's
- # still a string that needs to be copied properly:
- if x.typ.skipTypes(abstractInst).kind in {tySequence, tyOpt, tyString}:
- xtyp = etySeq
- case xtyp
- of etySeq:
- if (needsNoCopy(p, y) and needsNoCopy(p, x)) or noCopyNeeded:
- lineF(p, "$1 = $2;$n", [a.rdLoc, b.rdLoc])
- else:
- useMagic(p, "nimCopy")
- lineF(p, "$1 = nimCopy(null, $2, $3);$n",
- [a.rdLoc, b.res, genTypeInfo(p, y.typ)])
- of etyObject:
- if (needsNoCopy(p, y) and needsNoCopy(p, x)) or noCopyNeeded:
- lineF(p, "$1 = $2;$n", [a.rdLoc, b.rdLoc])
- else:
- useMagic(p, "nimCopy")
- lineF(p, "nimCopy($1, $2, $3);$n",
- [a.res, b.res, genTypeInfo(p, y.typ)])
- of etyBaseIndex:
- if a.typ != etyBaseIndex or b.typ != etyBaseIndex:
- if y.kind == nkCall:
- let tmp = p.getTemp(false)
- lineF(p, "var $1 = $4; $2 = $1[0]; $3 = $1[1];$n", [tmp, a.address, a.res, b.rdLoc])
- elif b.typ == etyBaseIndex:
- lineF(p, "$# = [$#, $#];$n", [a.res, b.address, b.res])
- else:
- internalError(p.config, x.info, "genAsgn")
- else:
- lineF(p, "$1 = $2; $3 = $4;$n", [a.address, b.address, a.res, b.res])
- else:
- lineF(p, "$1 = $2;$n", [a.rdLoc, b.rdLoc])
- proc genAsgn(p: PProc, n: PNode) =
- genAsgnAux(p, n.sons[0], n.sons[1], noCopyNeeded=false)
- proc genFastAsgn(p: PProc, n: PNode) =
- # 'shallowCopy' always produced 'noCopyNeeded = true' here but this is wrong
- # for code like
- # while j >= pos:
- # dest[i].shallowCopy(dest[j])
- # See bug #5933. So we try to be more compatible with the C backend semantics
- # here for 'shallowCopy'. This is an educated guess and might require further
- # changes later:
- let noCopy = n[0].typ.skipTypes(abstractInst).kind in {tySequence, tyOpt, tyString}
- genAsgnAux(p, n.sons[0], n.sons[1], noCopyNeeded=noCopy)
- proc genSwap(p: PProc, n: PNode) =
- var a, b: TCompRes
- gen(p, n.sons[1], a)
- gen(p, n.sons[2], b)
- var tmp = p.getTemp(false)
- if mapType(p, skipTypes(n.sons[1].typ, abstractVar)) == etyBaseIndex:
- let tmp2 = p.getTemp(false)
- if a.typ != etyBaseIndex or b.typ != etyBaseIndex:
- internalError(p.config, n.info, "genSwap")
- lineF(p, "var $1 = $2; $2 = $3; $3 = $1;$n",
- [tmp, a.address, b.address])
- tmp = tmp2
- lineF(p, "var $1 = $2; $2 = $3; $3 = $1;",
- [tmp, a.res, b.res])
- proc getFieldPosition(p: PProc; f: PNode): int =
- case f.kind
- of nkIntLit..nkUInt64Lit: result = int(f.intVal)
- of nkSym: result = f.sym.position
- else: internalError(p.config, f.info, "genFieldPosition")
- proc genFieldAddr(p: PProc, n: PNode, r: var TCompRes) =
- var a: TCompRes
- r.typ = etyBaseIndex
- let b = if n.kind == nkHiddenAddr: n.sons[0] else: n
- gen(p, b.sons[0], a)
- if skipTypes(b.sons[0].typ, abstractVarRange).kind == tyTuple:
- r.res = makeJSString("Field" & $getFieldPosition(p, b.sons[1]))
- else:
- if b.sons[1].kind != nkSym: internalError(p.config, b.sons[1].info, "genFieldAddr")
- var f = b.sons[1].sym
- if f.loc.r == nil: f.loc.r = mangleName(p.module, f)
- r.res = makeJSString($f.loc.r)
- internalAssert p.config, a.typ != etyBaseIndex
- r.address = a.res
- r.kind = resExpr
- proc genFieldAccess(p: PProc, n: PNode, r: var TCompRes) =
- gen(p, n.sons[0], r)
- r.typ = mapType(n.typ)
- let otyp = skipTypes(n.sons[0].typ, abstractVarRange)
- template mkTemp(i: int) =
- if r.typ == etyBaseIndex:
- if needsTemp(p, n[i]):
- let tmp = p.getTemp
- r.address = "($1 = $2, $1)[0]" % [tmp, r.res]
- r.res = "$1[1]" % [tmp]
- r.tmpLoc = tmp
- else:
- r.address = "$1[0]" % [r.res]
- r.res = "$1[1]" % [r.res]
- if otyp.kind == tyTuple:
- r.res = ("$1.Field$2") %
- [r.res, getFieldPosition(p, n.sons[1]).rope]
- mkTemp(0)
- else:
- if n.sons[1].kind != nkSym: internalError(p.config, n.sons[1].info, "genFieldAccess")
- var f = n.sons[1].sym
- if f.loc.r == nil: f.loc.r = mangleName(p.module, f)
- r.res = "$1.$2" % [r.res, f.loc.r]
- mkTemp(1)
- r.kind = resExpr
- proc genAddr(p: PProc, n: PNode, r: var TCompRes)
- proc genCheckedFieldOp(p: PProc, n: PNode, addrTyp: PType, r: var TCompRes) =
- internalAssert p.config, n.kind == nkCheckedFieldExpr
- # nkDotExpr to access the requested field
- let accessExpr = n[0]
- # nkCall to check if the discriminant is valid
- var checkExpr = n[1]
- let negCheck = checkExpr[0].sym.magic == mNot
- if negCheck:
- checkExpr = checkExpr[^1]
- # Field symbol
- var field = accessExpr[1].sym
- internalAssert p.config, field.kind == skField
- if field.loc.r == nil: field.loc.r = mangleName(p.module, field)
- # Discriminant symbol
- let disc = checkExpr[2].sym
- internalAssert p.config, disc.kind == skField
- if disc.loc.r == nil: disc.loc.r = mangleName(p.module, disc)
- var setx: TCompRes
- gen(p, checkExpr[1], setx)
- var obj: TCompRes
- gen(p, accessExpr[0], obj)
- # Avoid evaluating the LHS twice (one to read the discriminant and one to read
- # the field)
- let tmp = p.getTemp()
- lineF(p, "var $1 = $2;$n", tmp, obj.res)
- useMagic(p, "raiseFieldError")
- useMagic(p, "makeNimstrLit")
- lineF(p, "if ($1[$2.$3]$4undefined) { raiseFieldError(makeNimstrLit($5)); }$n",
- setx.res, tmp, disc.loc.r, if negCheck: ~"!==" else: ~"===",
- makeJSString(field.name.s))
- if addrTyp != nil and mapType(p, addrTyp) == etyBaseIndex:
- r.typ = etyBaseIndex
- r.res = makeJSString($field.loc.r)
- r.address = tmp
- else:
- r.typ = etyNone
- r.res = "$1.$2" % [tmp, field.loc.r]
- r.kind = resExpr
- proc genArrayAddr(p: PProc, n: PNode, r: var TCompRes) =
- var
- a, b: TCompRes
- first: BiggestInt
- r.typ = etyBaseIndex
- let m = if n.kind == nkHiddenAddr: n.sons[0] else: n
- gen(p, m.sons[0], a)
- gen(p, m.sons[1], b)
- #internalAssert p.config, a.typ != etyBaseIndex and b.typ != etyBaseIndex
- let (x, tmp) = maybeMakeTemp(p, m[0], a)
- r.address = x
- var typ = skipTypes(m.sons[0].typ, abstractPtrs)
- if typ.kind == tyArray: first = firstOrd(p.config, typ.sons[0])
- else: first = 0
- if optBoundsCheck in p.options:
- useMagic(p, "chckIndx")
- r.res = "chckIndx($1, $2, $3.length+$2-1)-$2" % [b.res, rope(first), tmp]
- elif first != 0:
- r.res = "($1)-$2" % [b.res, rope(first)]
- else:
- r.res = b.res
- r.kind = resExpr
- proc genArrayAccess(p: PProc, n: PNode, r: var TCompRes) =
- var ty = skipTypes(n.sons[0].typ, abstractVarRange)
- if ty.kind in {tyRef, tyPtr, tyLent}: ty = skipTypes(ty.lastSon, abstractVarRange)
- case ty.kind
- of tyArray, tyOpenArray, tySequence, tyString, tyCString, tyVarargs:
- genArrayAddr(p, n, r)
- of tyTuple:
- genFieldAddr(p, n, r)
- else: internalError(p.config, n.info, "expr(nkBracketExpr, " & $ty.kind & ')')
- r.typ = mapType(n.typ)
- if r.res == nil: internalError(p.config, n.info, "genArrayAccess")
- if ty.kind == tyCString:
- r.res = "$1.charCodeAt($2)" % [r.address, r.res]
- elif r.typ == etyBaseIndex:
- if needsTemp(p, n[0]):
- let tmp = p.getTemp
- r.address = "($1 = $2, $1)[0]" % [tmp, r.rdLoc]
- r.res = "$1[1]" % [tmp]
- r.tmpLoc = tmp
- else:
- let x = r.rdLoc
- r.address = "$1[0]" % [x]
- r.res = "$1[1]" % [x]
- else:
- r.res = "$1[$2]" % [r.address, r.res]
- r.kind = resExpr
- template isIndirect(x: PSym): bool =
- let v = x
- ({sfAddrTaken, sfGlobal} * v.flags != {} and
- #(mapType(v.typ) != etyObject) and
- {sfImportc, sfVolatile, sfExportc} * v.flags == {} and
- v.kind notin {skProc, skFunc, skConverter, skMethod, skIterator,
- skConst, skTemp, skLet})
- proc genAddr(p: PProc, n: PNode, r: var TCompRes) =
- case n.sons[0].kind
- of nkSym:
- let s = n.sons[0].sym
- if s.loc.r == nil: internalError(p.config, n.info, "genAddr: 3")
- case s.kind
- of skVar, skLet, skResult:
- r.kind = resExpr
- let jsType = mapType(p, n.typ)
- if jsType == etyObject:
- # make addr() a no-op:
- r.typ = etyNone
- if isIndirect(s):
- r.res = s.loc.r & "[0]"
- else:
- r.res = s.loc.r
- r.address = nil
- elif {sfGlobal, sfAddrTaken} * s.flags != {} or jsType == etyBaseIndex:
- # for ease of code generation, we do not distinguish between
- # sfAddrTaken and sfGlobal.
- r.typ = etyBaseIndex
- r.address = s.loc.r
- r.res = rope("0")
- else:
- # 'var openArray' for instance produces an 'addr' but this is harmless:
- gen(p, n.sons[0], r)
- #internalError(p.config, n.info, "genAddr: 4 " & renderTree(n))
- else: internalError(p.config, n.info, "genAddr: 2")
- of nkCheckedFieldExpr:
- genCheckedFieldOp(p, n[0], n.typ, r)
- of nkDotExpr:
- if mapType(p, n.typ) == etyBaseIndex:
- genFieldAddr(p, n.sons[0], r)
- else:
- genFieldAccess(p, n.sons[0], r)
- of nkBracketExpr:
- var ty = skipTypes(n.sons[0].typ, abstractVarRange)
- if ty.kind in MappedToObject:
- gen(p, n.sons[0], r)
- else:
- let kindOfIndexedExpr = skipTypes(n.sons[0].sons[0].typ, abstractVarRange).kind
- case kindOfIndexedExpr
- of tyArray, tyOpenArray, tySequence, tyString, tyCString, tyVarargs:
- genArrayAddr(p, n.sons[0], r)
- of tyTuple:
- genFieldAddr(p, n.sons[0], r)
- else: internalError(p.config, n.sons[0].info, "expr(nkBracketExpr, " & $kindOfIndexedExpr & ')')
- of nkObjDownConv:
- gen(p, n.sons[0], r)
- of nkHiddenDeref:
- gen(p, n.sons[0].sons[0], r)
- else: internalError(p.config, n.sons[0].info, "genAddr: " & $n.sons[0].kind)
- proc thisParam(p: PProc; typ: PType): PType =
- discard
- proc attachProc(p: PProc; content: Rope; s: PSym) =
- let otyp = thisParam(p, s.typ)
- if otyp != nil:
- for i, cls in p.g.classes:
- if sameType(cls[0], otyp):
- add(p.g.classes[i][1], content)
- return
- p.g.classes.add((otyp, content))
- else:
- add(p.g.code, content)
- proc attachProc(p: PProc; s: PSym) =
- let newp = genProc(p, s)
- attachProc(p, newp, s)
- proc genProcForSymIfNeeded(p: PProc, s: PSym) =
- if not p.g.generatedSyms.containsOrIncl(s.id):
- let newp = genProc(p, s)
- var owner = p
- while owner != nil and owner.prc != s.owner:
- owner = owner.up
- if owner != nil: add(owner.locals, newp)
- else: attachProc(p, newp, s)
- proc genSym(p: PProc, n: PNode, r: var TCompRes) =
- var s = n.sym
- case s.kind
- of skVar, skLet, skParam, skTemp, skResult, skForVar:
- if s.loc.r == nil:
- internalError(p.config, n.info, "symbol has no generated name: " & s.name.s)
- let k = mapType(p, s.typ)
- if k == etyBaseIndex:
- r.typ = etyBaseIndex
- if {sfAddrTaken, sfGlobal} * s.flags != {}:
- if isIndirect(s):
- r.address = "$1[0][0]" % [s.loc.r]
- r.res = "$1[0][1]" % [s.loc.r]
- else:
- r.address = "$1[0]" % [s.loc.r]
- r.res = "$1[1]" % [s.loc.r]
- else:
- r.address = s.loc.r
- r.res = s.loc.r & "_Idx"
- elif isIndirect(s):
- r.res = "$1[0]" % [s.loc.r]
- else:
- r.res = s.loc.r
- of skConst:
- genConstant(p, s)
- if s.loc.r == nil:
- internalError(p.config, n.info, "symbol has no generated name: " & s.name.s)
- r.res = s.loc.r
- of skProc, skFunc, skConverter, skMethod:
- discard mangleName(p.module, s)
- r.res = s.loc.r
- if lfNoDecl in s.loc.flags or s.magic != mNone or
- {sfImportc, sfInfixCall} * s.flags != {}:
- discard
- elif s.kind == skMethod and s.getBody.kind == nkEmpty:
- # we cannot produce code for the dispatcher yet:
- discard
- elif sfForward in s.flags:
- p.g.forwarded.add(s)
- else:
- genProcForSymIfNeeded(p, s)
- else:
- if s.loc.r == nil:
- internalError(p.config, n.info, "symbol has no generated name: " & s.name.s)
- r.res = s.loc.r
- r.kind = resVal
- proc genDeref(p: PProc, n: PNode, r: var TCompRes) =
- let it = n.sons[0]
- let t = mapType(p, it.typ)
- if t == etyObject:
- gen(p, it, r)
- else:
- var a: TCompRes
- gen(p, it, a)
- r.kind = a.kind
- r.typ = mapType(p, n.typ)
- if r.typ == etyBaseIndex:
- let tmp = p.getTemp
- r.address = "($1 = $2, $1)[0]" % [tmp, a.rdLoc]
- r.res = "$1[1]" % [tmp]
- r.tmpLoc = tmp
- elif a.typ == etyBaseIndex:
- if a.tmpLoc != nil:
- r.tmpLoc = a.tmpLoc
- r.res = a.rdLoc
- else:
- internalError(p.config, n.info, "genDeref")
- proc genArgNoParam(p: PProc, n: PNode, r: var TCompRes) =
- var a: TCompRes
- gen(p, n, a)
- if a.typ == etyBaseIndex:
- add(r.res, a.address)
- add(r.res, ", ")
- add(r.res, a.res)
- else:
- add(r.res, a.res)
- proc genArg(p: PProc, n: PNode, param: PSym, r: var TCompRes; emitted: ptr int = nil) =
- var a: TCompRes
- gen(p, n, a)
- if skipTypes(param.typ, abstractVar).kind in {tyOpenArray, tyVarargs} and
- a.typ == etyBaseIndex:
- add(r.res, "$1[$2]" % [a.address, a.res])
- elif a.typ == etyBaseIndex:
- add(r.res, a.address)
- add(r.res, ", ")
- add(r.res, a.res)
- if emitted != nil: inc emitted[]
- elif n.typ.kind in {tyVar, tyPtr, tyRef, tyLent} and n.kind in nkCallKinds and mapType(param.typ) == etyBaseIndex:
- # this fixes bug #5608:
- let tmp = getTemp(p)
- add(r.res, "($1 = $2, $1[0]), $1[1]" % [tmp, a.rdLoc])
- if emitted != nil: inc emitted[]
- else:
- add(r.res, a.res)
- proc genArgs(p: PProc, n: PNode, r: var TCompRes; start=1) =
- add(r.res, "(")
- var hasArgs = false
- var typ = skipTypes(n.sons[0].typ, abstractInst)
- assert(typ.kind == tyProc)
- assert(sonsLen(typ) == sonsLen(typ.n))
- var emitted = start-1
- for i in countup(start, sonsLen(n) - 1):
- let it = n.sons[i]
- var paramType: PNode = nil
- if i < sonsLen(typ):
- assert(typ.n.sons[i].kind == nkSym)
- paramType = typ.n.sons[i]
- if paramType.typ.isCompileTimeOnly: continue
- if hasArgs: add(r.res, ", ")
- if paramType.isNil:
- genArgNoParam(p, it, r)
- else:
- genArg(p, it, paramType.sym, r, addr emitted)
- inc emitted
- hasArgs = true
- add(r.res, ")")
- when false:
- # XXX look into this:
- let jsp = countJsParams(typ)
- if emitted != jsp and tfVarargs notin typ.flags:
- localError(p.config, n.info, "wrong number of parameters emitted; expected: " & $jsp &
- " but got: " & $emitted)
- r.kind = resExpr
- proc genOtherArg(p: PProc; n: PNode; i: int; typ: PType;
- generated: var int; r: var TCompRes) =
- if i >= n.len:
- globalError(p.config, n.info, "wrong importcpp pattern; expected parameter at position " & $i &
- " but got only: " & $(n.len-1))
- let it = n[i]
- var paramType: PNode = nil
- if i < sonsLen(typ):
- assert(typ.n.sons[i].kind == nkSym)
- paramType = typ.n.sons[i]
- if paramType.typ.isCompileTimeOnly: return
- if paramType.isNil:
- genArgNoParam(p, it, r)
- else:
- genArg(p, it, paramType.sym, r)
- inc generated
- proc genPatternCall(p: PProc; n: PNode; pat: string; typ: PType;
- r: var TCompRes) =
- var i = 0
- var j = 1
- r.kind = resExpr
- while i < pat.len:
- case pat[i]
- of '@':
- var generated = 0
- for k in j ..< n.len:
- if generated > 0: add(r.res, ", ")
- genOtherArg(p, n, k, typ, generated, r)
- inc i
- of '#':
- var generated = 0
- genOtherArg(p, n, j, typ, generated, r)
- inc j
- inc i
- of '\31':
- # unit separator
- add(r.res, "#")
- inc i
- of '\29':
- # group separator
- add(r.res, "@")
- inc i
- else:
- let start = i
- while i < pat.len:
- if pat[i] notin {'@', '#', '\31', '\29'}: inc(i)
- else: break
- if i - 1 >= start:
- add(r.res, substr(pat, start, i - 1))
- proc genInfixCall(p: PProc, n: PNode, r: var TCompRes) =
- # don't call '$' here for efficiency:
- let f = n[0].sym
- if f.loc.r == nil: f.loc.r = mangleName(p.module, f)
- if sfInfixCall in f.flags:
- let pat = n.sons[0].sym.loc.r.data
- internalAssert p.config, pat.len > 0
- if pat.contains({'#', '(', '@'}):
- var typ = skipTypes(n.sons[0].typ, abstractInst)
- assert(typ.kind == tyProc)
- genPatternCall(p, n, pat, typ, r)
- return
- if n.len != 1:
- gen(p, n.sons[1], r)
- if r.typ == etyBaseIndex:
- if r.address == nil:
- globalError(p.config, n.info, "cannot invoke with infix syntax")
- r.res = "$1[$2]" % [r.address, r.res]
- r.address = nil
- r.typ = etyNone
- add(r.res, ".")
- var op: TCompRes
- gen(p, n.sons[0], op)
- add(r.res, op.res)
- genArgs(p, n, r, 2)
- proc genCall(p: PProc, n: PNode, r: var TCompRes) =
- if n.sons[0].kind == nkSym and thisParam(p, n.sons[0].typ) != nil:
- genInfixCall(p, n, r)
- return
- gen(p, n.sons[0], r)
- genArgs(p, n, r)
- if n.typ != nil:
- let t = mapType(n.typ)
- if t == etyBaseIndex:
- let tmp = p.getTemp
- r.address = "($1 = $2, $1)[0]" % [tmp, r.rdLoc]
- r.res = "$1[1]" % [tmp]
- r.tmpLoc = tmp
- r.typ = t
- proc genEcho(p: PProc, n: PNode, r: var TCompRes) =
- let n = n[1].skipConv
- internalAssert p.config, n.kind == nkBracket
- useMagic(p, "toJSStr") # Used in rawEcho
- useMagic(p, "rawEcho")
- add(r.res, "rawEcho(")
- for i in countup(0, sonsLen(n) - 1):
- let it = n.sons[i]
- if it.typ.isCompileTimeOnly: continue
- if i > 0: add(r.res, ", ")
- genArgNoParam(p, it, r)
- add(r.res, ")")
- r.kind = resExpr
- proc putToSeq(s: string, indirect: bool): Rope =
- result = rope(s)
- if indirect: result = "[$1]" % [result]
- proc createVar(p: PProc, typ: PType, indirect: bool): Rope
- proc createRecordVarAux(p: PProc, rec: PNode, excludedFieldIDs: IntSet, output: var Rope) =
- case rec.kind
- of nkRecList:
- for i in countup(0, sonsLen(rec) - 1):
- createRecordVarAux(p, rec.sons[i], excludedFieldIDs, output)
- of nkRecCase:
- createRecordVarAux(p, rec.sons[0], excludedFieldIDs, output)
- for i in countup(1, sonsLen(rec) - 1):
- createRecordVarAux(p, lastSon(rec.sons[i]), excludedFieldIDs, output)
- of nkSym:
- if rec.sym.id notin excludedFieldIDs:
- if output.len > 0: output.add(", ")
- output.addf("$#: ", [mangleName(p.module, rec.sym)])
- output.add(createVar(p, rec.sym.typ, false))
- else: internalError(p.config, rec.info, "createRecordVarAux")
- proc createObjInitList(p: PProc, typ: PType, excludedFieldIDs: IntSet, output: var Rope) =
- var t = typ
- if objHasTypeField(t):
- if output.len > 0: output.add(", ")
- addf(output, "m_type: $1", [genTypeInfo(p, t)])
- while t != nil:
- t = t.skipTypes(skipPtrs)
- createRecordVarAux(p, t.n, excludedFieldIDs, output)
- t = t.sons[0]
- proc arrayTypeForElemType(typ: PType): string =
- # XXX This should also support tyEnum and tyBool
- case typ.kind
- of tyInt, tyInt32: "Int32Array"
- of tyInt16: "Int16Array"
- of tyInt8: "Int8Array"
- of tyUint, tyUint32: "Uint32Array"
- of tyUint16: "Uint16Array"
- of tyUint8: "Uint8Array"
- of tyFloat32: "Float32Array"
- of tyFloat64, tyFloat: "Float64Array"
- else: ""
- proc createVar(p: PProc, typ: PType, indirect: bool): Rope =
- var t = skipTypes(typ, abstractInst)
- case t.kind
- of tyInt..tyInt64, tyUInt..tyUInt64, tyEnum, tyChar:
- result = putToSeq("0", indirect)
- of tyFloat..tyFloat128:
- result = putToSeq("0.0", indirect)
- of tyRange, tyGenericInst, tyAlias, tySink:
- result = createVar(p, lastSon(typ), indirect)
- of tySet:
- result = putToSeq("{}", indirect)
- of tyBool:
- result = putToSeq("false", indirect)
- of tyArray:
- let length = int(lengthOrd(p.config, t))
- let e = elemType(t)
- let jsTyp = arrayTypeForElemType(e)
- if jsTyp.len > 0:
- result = "new $1($2)" % [rope(jsTyp), rope(length)]
- elif length > 32:
- useMagic(p, "arrayConstr")
- # XXX: arrayConstr depends on nimCopy. This line shouldn't be necessary.
- useMagic(p, "nimCopy")
- result = "arrayConstr($1, $2, $3)" % [rope(length),
- createVar(p, e, false), genTypeInfo(p, e)]
- else:
- result = rope("[")
- var i = 0
- while i < length:
- if i > 0: add(result, ", ")
- add(result, createVar(p, e, false))
- inc(i)
- add(result, "]")
- if indirect: result = "[$1]" % [result]
- of tyTuple:
- result = rope("{")
- for i in 0..<t.sonsLen:
- if i > 0: add(result, ", ")
- addf(result, "Field$1: $2", [i.rope,
- createVar(p, t.sons[i], false)])
- add(result, "}")
- if indirect: result = "[$1]" % [result]
- of tyObject:
- var initList: Rope
- createObjInitList(p, t, initIntSet(), initList)
- result = ("{$1}") % [initList]
- if indirect: result = "[$1]" % [result]
- of tyVar, tyPtr, tyLent, tyRef, tyPointer:
- if mapType(p, t) == etyBaseIndex:
- result = putToSeq("[null, 0]", indirect)
- else:
- result = putToSeq("null", indirect)
- of tySequence, tyOpt, tyString, tyCString, tyProc:
- result = putToSeq("null", indirect)
- of tyStatic:
- if t.n != nil:
- result = createVar(p, lastSon t, indirect)
- else:
- internalError(p.config, "createVar: " & $t.kind)
- result = nil
- else:
- internalError(p.config, "createVar: " & $t.kind)
- result = nil
- template returnType: untyped =
- ~""
- proc genVarInit(p: PProc, v: PSym, n: PNode) =
- var
- a: TCompRes
- s: Rope
- varCode: string
- varName = mangleName(p.module, v)
- useReloadingGuard = sfGlobal in v.flags and optHotCodeReloading in p.config.options
- if v.constraint.isNil:
- if useReloadingGuard:
- lineF(p, "var $1;$n", varName)
- lineF(p, "if ($1 === undefined) {$n", varName)
- varCode = $varName
- else:
- varCode = "var $2"
- else:
- varCode = v.constraint.strVal
- if n.kind == nkEmpty:
- if not isIndirect(v) and
- v.typ.kind in {tyVar, tyPtr, tyLent, tyRef} and mapType(p, v.typ) == etyBaseIndex:
- lineF(p, "var $1 = null;$n", [varName])
- lineF(p, "var $1_Idx = 0;$n", [varName])
- else:
- lineF(p, varCode & " = $3;$n",
- [returnType, varName, createVar(p, v.typ, isIndirect(v))])
- else:
- gen(p, n, a)
- case mapType(p, v.typ)
- of etyObject, etySeq:
- if needsNoCopy(p, n):
- s = a.res
- else:
- useMagic(p, "nimCopy")
- s = "nimCopy(null, $1, $2)" % [a.res, genTypeInfo(p, n.typ)]
- of etyBaseIndex:
- let targetBaseIndex = {sfAddrTaken, sfGlobal} * v.flags == {}
- if a.typ == etyBaseIndex:
- if targetBaseIndex:
- lineF(p, varCode & " = $3, $2_Idx = $4;$n",
- [returnType, v.loc.r, a.address, a.res])
- else:
- if isIndirect(v):
- lineF(p, varCode & " = [[$3, $4]];$n",
- [returnType, v.loc.r, a.address, a.res])
- else:
- lineF(p, varCode & " = [$3, $4];$n",
- [returnType, v.loc.r, a.address, a.res])
- else:
- if targetBaseIndex:
- let tmp = p.getTemp
- lineF(p, "var $1 = $2, $3 = $1[0], $3_Idx = $1[1];$n",
- [tmp, a.res, v.loc.r])
- else:
- lineF(p, varCode & " = $3;$n", [returnType, v.loc.r, a.res])
- return
- else:
- s = a.res
- if isIndirect(v):
- lineF(p, varCode & " = [$3];$n", [returnType, v.loc.r, s])
- else:
- lineF(p, varCode & " = $3;$n", [returnType, v.loc.r, s])
- if useReloadingGuard:
- lineF(p, "}$n")
- proc genVarStmt(p: PProc, n: PNode) =
- for i in countup(0, sonsLen(n) - 1):
- var a = n.sons[i]
- if a.kind != nkCommentStmt:
- if a.kind == nkVarTuple:
- let unpacked = lowerTupleUnpacking(p.module.graph, a, p.prc)
- genStmt(p, unpacked)
- else:
- assert(a.kind == nkIdentDefs)
- assert(a.sons[0].kind == nkSym)
- var v = a.sons[0].sym
- if lfNoDecl notin v.loc.flags and sfImportc notin v.flags:
- genLineDir(p, a)
- genVarInit(p, v, a.sons[2])
- proc genConstant(p: PProc, c: PSym) =
- if lfNoDecl notin c.loc.flags and not p.g.generatedSyms.containsOrIncl(c.id):
- let oldBody = p.body
- p.body = nil
- #genLineDir(p, c.ast)
- genVarInit(p, c, c.ast)
- add(p.g.constants, p.body)
- p.body = oldBody
- proc genNew(p: PProc, n: PNode) =
- var a: TCompRes
- gen(p, n.sons[1], a)
- var t = skipTypes(n.sons[1].typ, abstractVar).sons[0]
- if mapType(t) == etyObject:
- lineF(p, "$1 = $2;$n", [a.rdLoc, createVar(p, t, false)])
- elif a.typ == etyBaseIndex:
- lineF(p, "$1 = [$3]; $2 = 0;$n", [a.address, a.res, createVar(p, t, false)])
- else:
- lineF(p, "$1 = [[$2], 0];$n", [a.rdLoc, createVar(p, t, false)])
- proc genNewSeq(p: PProc, n: PNode) =
- var x, y: TCompRes
- gen(p, n.sons[1], x)
- gen(p, n.sons[2], y)
- let t = skipTypes(n.sons[1].typ, abstractVar).sons[0]
- lineF(p, "$1 = new Array($2); for (var i=0;i<$2;++i) {$1[i]=$3;}", [
- x.rdLoc, y.rdLoc, createVar(p, t, false)])
- proc genOrd(p: PProc, n: PNode, r: var TCompRes) =
- case skipTypes(n.sons[1].typ, abstractVar).kind
- of tyEnum, tyInt..tyUInt64, tyChar: gen(p, n.sons[1], r)
- of tyBool: unaryExpr(p, n, r, "", "($1 ? 1:0)")
- else: internalError(p.config, n.info, "genOrd")
- proc genConStrStr(p: PProc, n: PNode, r: var TCompRes) =
- var a: TCompRes
- gen(p, n.sons[1], a)
- r.kind = resExpr
- if skipTypes(n.sons[1].typ, abstractVarRange).kind == tyChar:
- r.res.add("[$1].concat(" % [a.res])
- else:
- r.res.add("($1 || []).concat(" % [a.res])
- for i in countup(2, sonsLen(n) - 2):
- gen(p, n.sons[i], a)
- if skipTypes(n.sons[i].typ, abstractVarRange).kind == tyChar:
- r.res.add("[$1]," % [a.res])
- else:
- r.res.add("$1 || []," % [a.res])
- gen(p, n.sons[sonsLen(n) - 1], a)
- if skipTypes(n.sons[sonsLen(n) - 1].typ, abstractVarRange).kind == tyChar:
- r.res.add("[$1])" % [a.res])
- else:
- r.res.add("$1 || [])" % [a.res])
- proc genToArray(p: PProc; n: PNode; r: var TCompRes) =
- # we map mArray to PHP's array constructor, a mild hack:
- var a, b: TCompRes
- r.kind = resExpr
- r.res = rope("array(")
- let x = skipConv(n[1])
- if x.kind == nkBracket:
- for i in countup(0, x.len - 1):
- let it = x[i]
- if it.kind in {nkPar, nkTupleConstr} and it.len == 2:
- if i > 0: r.res.add(", ")
- gen(p, it[0], a)
- gen(p, it[1], b)
- r.res.add("$# => $#" % [a.rdLoc, b.rdLoc])
- else:
- localError(p.config, it.info, "'toArray' needs tuple constructors")
- else:
- localError(p.config, x.info, "'toArray' needs an array literal")
- r.res.add(")")
- proc genReprAux(p: PProc, n: PNode, r: var TCompRes, magic: string, typ: Rope = nil) =
- useMagic(p, magic)
- add(r.res, magic & "(")
- var a: TCompRes
- gen(p, n.sons[1], a)
- if magic == "reprAny":
- # the pointer argument in reprAny is expandend to
- # (pointedto, pointer), so we need to fill it
- if a.address.isNil:
- add(r.res, a.res)
- add(r.res, ", null")
- else:
- add(r.res, "$1, $2" % [a.address, a.res])
- else:
- add(r.res, a.res)
- if not typ.isNil:
- add(r.res, ", ")
- add(r.res, typ)
- add(r.res, ")")
- proc genRepr(p: PProc, n: PNode, r: var TCompRes) =
- let t = skipTypes(n.sons[1].typ, abstractVarRange)
- case t.kind:
- of tyInt..tyInt64, tyUInt..tyUInt64:
- genReprAux(p, n, r, "reprInt")
- of tyChar:
- genReprAux(p, n, r, "reprChar")
- of tyBool:
- genReprAux(p, n, r, "reprBool")
- of tyFloat..tyFloat128:
- genReprAux(p, n, r, "reprFloat")
- of tyString:
- genReprAux(p, n, r, "reprStr")
- of tyEnum, tyOrdinal:
- genReprAux(p, n, r, "reprEnum", genTypeInfo(p, t))
- of tySet:
- genReprAux(p, n, r, "reprSet", genTypeInfo(p, t))
- of tyEmpty, tyVoid:
- localError(p.config, n.info, "'repr' doesn't support 'void' type")
- of tyPointer:
- genReprAux(p, n, r, "reprPointer")
- of tyOpenArray, tyVarargs:
- genReprAux(p, n, r, "reprJSONStringify")
- else:
- genReprAux(p, n, r, "reprAny", genTypeInfo(p, t))
- proc genOf(p: PProc, n: PNode, r: var TCompRes) =
- var x: TCompRes
- let t = skipTypes(n.sons[2].typ, abstractVarRange+{tyRef, tyPtr, tyLent, tyTypeDesc})
- gen(p, n.sons[1], x)
- if tfFinal in t.flags:
- r.res = "($1.m_type == $2)" % [x.res, genTypeInfo(p, t)]
- else:
- useMagic(p, "isObj")
- r.res = "isObj($1.m_type, $2)" % [x.res, genTypeInfo(p, t)]
- r.kind = resExpr
- proc genReset(p: PProc, n: PNode) =
- var x: TCompRes
- useMagic(p, "genericReset")
- gen(p, n.sons[1], x)
- if x.typ == etyBaseIndex:
- lineF(p, "$1 = null, $2 = 0;$n", [x.address, x.res])
- else:
- let (a, tmp) = maybeMakeTemp(p, n[1], x)
- lineF(p, "$1 = genericReset($3, $2);$n", [a,
- genTypeInfo(p, n.sons[1].typ), tmp])
- proc genMagic(p: PProc, n: PNode, r: var TCompRes) =
- var
- a: TCompRes
- line, filen: Rope
- var op = n.sons[0].sym.magic
- case op
- of mOr: genOr(p, n.sons[1], n.sons[2], r)
- of mAnd: genAnd(p, n.sons[1], n.sons[2], r)
- of mAddI..mStrToStr: arith(p, n, r, op)
- of mRepr: genRepr(p, n, r)
- of mSwap: genSwap(p, n)
- of mUnaryLt:
- # XXX: range checking?
- if not (optOverflowCheck in p.options): unaryExpr(p, n, r, "", "$1 - 1")
- else: unaryExpr(p, n, r, "subInt", "subInt($1, 1)")
- of mAppendStrCh:
- binaryExpr(p, n, r, "addChar",
- "if ($1 != null) { addChar($3, $2); } else { $3 = [$2]; }")
- of mAppendStrStr:
- var lhs, rhs: TCompRes
- gen(p, n[1], lhs)
- gen(p, n[2], rhs)
- let rhsIsLit = n[2].kind in nkStrKinds
- let (a, tmp) = maybeMakeTemp(p, n[1], lhs)
- if skipTypes(n.sons[1].typ, abstractVarRange).kind == tyCString:
- r.res = "if ($1 != null) { $4 += $2; } else { $4 = $2$3; }" % [
- a, rhs.rdLoc, if rhsIsLit: nil else: ~".slice()", tmp]
- else:
- r.res = "if ($1 != null) { $4 = ($4).concat($2); } else { $4 = $2$3; }" % [
- lhs.rdLoc, rhs.rdLoc, if rhsIsLit: nil else: ~".slice()", tmp]
- r.kind = resExpr
- of mAppendSeqElem:
- var x, y: TCompRes
- gen(p, n.sons[1], x)
- gen(p, n.sons[2], y)
- let (a, tmp) = maybeMakeTemp(p, n[1], x)
- if mapType(n[2].typ) == etyBaseIndex:
- let c = "[$1, $2]" % [y.address, y.res]
- r.res = "if ($1 != null) { $3.push($2); } else { $3 = [$2]; }" % [a, c, tmp]
- elif needsNoCopy(p, n[2]):
- r.res = "if ($1 != null) { $3.push($2); } else { $3 = [$2]; }" % [a, y.rdLoc, tmp]
- else:
- useMagic(p, "nimCopy")
- let c = getTemp(p, defineInLocals=false)
- lineF(p, "var $1 = nimCopy(null, $2, $3);$n",
- [c, y.rdLoc, genTypeInfo(p, n[2].typ)])
- r.res = "if ($1 != null) { $3.push($2); } else { $3 = [$2]; }" % [a, c, tmp]
- r.kind = resExpr
- of mConStrStr:
- genConStrStr(p, n, r)
- of mEqStr:
- binaryExpr(p, n, r, "eqStrings", "eqStrings($1, $2)")
- of mLeStr:
- binaryExpr(p, n, r, "cmpStrings", "(cmpStrings($1, $2) <= 0)")
- of mLtStr:
- binaryExpr(p, n, r, "cmpStrings", "(cmpStrings($1, $2) < 0)")
- of mIsNil:
- if mapType(n[1].typ) != etyBaseIndex:
- unaryExpr(p, n, r, "", "($1 === null)")
- else:
- var x: TCompRes
- gen(p, n[1], x)
- r.res = "($# === null && $# === 0)" % [x.address, x.res]
- of mEnumToStr: genRepr(p, n, r)
- of mNew, mNewFinalize: genNew(p, n)
- of mChr: gen(p, n.sons[1], r)
- of mArrToSeq:
- if needsNoCopy(p, n.sons[1]):
- gen(p, n.sons[1], r)
- else:
- var x: TCompRes
- gen(p, n.sons[1], x)
- useMagic(p, "nimCopy")
- r.res = "nimCopy(null, $1, $2)" % [x.rdLoc, genTypeInfo(p, n.typ)]
- of mDestroy: discard "ignore calls to the default destructor"
- of mOrd: genOrd(p, n, r)
- of mLengthStr, mLengthSeq, mLengthOpenArray, mLengthArray:
- unaryExpr(p, n, r, "", "($1 != null ? $2.length : 0)")
- of mXLenStr, mXLenSeq:
- unaryExpr(p, n, r, "", "$1.length")
- of mHigh:
- unaryExpr(p, n, r, "", "($1 != null ? ($2.length-1) : -1)")
- of mInc:
- if n[1].typ.skipTypes(abstractRange).kind in tyUInt .. tyUInt64:
- binaryUintExpr(p, n, r, "+", true)
- else:
- if optOverflowCheck notin p.options: binaryExpr(p, n, r, "", "$1 += $2")
- else: binaryExpr(p, n, r, "addInt", "$1 = addInt($3, $2)")
- of ast.mDec:
- if n[1].typ.skipTypes(abstractRange).kind in tyUInt .. tyUInt64:
- binaryUintExpr(p, n, r, "-", true)
- else:
- if optOverflowCheck notin p.options: binaryExpr(p, n, r, "", "$1 -= $2")
- else: binaryExpr(p, n, r, "subInt", "$1 = subInt($3, $2)")
- of mSetLengthStr:
- binaryExpr(p, n, r, "mnewString", "($1 === null ? $3 = mnewString($2) : $3.length = $2)")
- of mSetLengthSeq:
- var x, y: TCompRes
- gen(p, n.sons[1], x)
- gen(p, n.sons[2], y)
- let t = skipTypes(n.sons[1].typ, abstractVar).sons[0]
- let (a, tmp) = maybeMakeTemp(p, n[1], x)
- let (b, tmp2) = maybeMakeTemp(p, n[2], y)
- r.res = """if ($1 === null) $4 = [];
- if ($4.length < $2) { for (var i=$4.length;i<$5;++i) $4.push($3); }
- else { $4.length = $5; }""" % [a, b, createVar(p, t, false), tmp, tmp2]
- r.kind = resExpr
- of mCard: unaryExpr(p, n, r, "SetCard", "SetCard($1)")
- of mLtSet: binaryExpr(p, n, r, "SetLt", "SetLt($1, $2)")
- of mLeSet: binaryExpr(p, n, r, "SetLe", "SetLe($1, $2)")
- of mEqSet: binaryExpr(p, n, r, "SetEq", "SetEq($1, $2)")
- of mMulSet: binaryExpr(p, n, r, "SetMul", "SetMul($1, $2)")
- of mPlusSet: binaryExpr(p, n, r, "SetPlus", "SetPlus($1, $2)")
- of mMinusSet: binaryExpr(p, n, r, "SetMinus", "SetMinus($1, $2)")
- of mIncl: binaryExpr(p, n, r, "", "$1[$2] = true")
- of mExcl: binaryExpr(p, n, r, "", "delete $1[$2]")
- of mInSet:
- binaryExpr(p, n, r, "", "($1[$2] != undefined)")
- of mNewSeq: genNewSeq(p, n)
- of mNewSeqOfCap: unaryExpr(p, n, r, "", "[]")
- of mOf: genOf(p, n, r)
- of mReset: genReset(p, n)
- of mEcho: genEcho(p, n, r)
- of mNLen..mNError, mSlurp, mStaticExec:
- localError(p.config, n.info, errXMustBeCompileTime % n.sons[0].sym.name.s)
- of mCopyStr:
- binaryExpr(p, n, r, "", "($1.slice($2))")
- of mNewString: unaryExpr(p, n, r, "mnewString", "mnewString($1)")
- of mNewStringOfCap:
- unaryExpr(p, n, r, "mnewString", "mnewString(0)")
- of mDotDot:
- genProcForSymIfNeeded(p, n.sons[0].sym)
- genCall(p, n, r)
- of mParseBiggestFloat:
- useMagic(p, "nimParseBiggestFloat")
- genCall(p, n, r)
- of mArray:
- genCall(p, n, r)
- else:
- genCall(p, n, r)
- #else internalError(p.config, e.info, 'genMagic: ' + magicToStr[op]);
- proc genSetConstr(p: PProc, n: PNode, r: var TCompRes) =
- var
- a, b: TCompRes
- useMagic(p, "setConstr")
- r.res = rope("setConstr(")
- r.kind = resExpr
- for i in countup(0, sonsLen(n) - 1):
- if i > 0: add(r.res, ", ")
- var it = n.sons[i]
- if it.kind == nkRange:
- gen(p, it.sons[0], a)
- gen(p, it.sons[1], b)
- addf(r.res, "[$1, $2]", [a.res, b.res])
- else:
- gen(p, it, a)
- add(r.res, a.res)
- add(r.res, ")")
- # emit better code for constant sets:
- if isDeepConstExpr(n):
- inc(p.g.unique)
- let tmp = rope("ConstSet") & rope(p.g.unique)
- addf(p.g.constants, "var $1 = $2;$n", [tmp, r.res])
- r.res = tmp
- proc genArrayConstr(p: PProc, n: PNode, r: var TCompRes) =
- var a: TCompRes
- r.res = rope("[")
- r.kind = resExpr
- for i in countup(0, sonsLen(n) - 1):
- if i > 0: add(r.res, ", ")
- gen(p, n.sons[i], a)
- if a.typ == etyBaseIndex:
- addf(r.res, "[$1, $2]", [a.address, a.res])
- else:
- add(r.res, a.res)
- add(r.res, "]")
- proc genTupleConstr(p: PProc, n: PNode, r: var TCompRes) =
- var a: TCompRes
- r.res = rope("{")
- r.kind = resExpr
- for i in countup(0, sonsLen(n) - 1):
- if i > 0: add(r.res, ", ")
- var it = n.sons[i]
- if it.kind == nkExprColonExpr: it = it.sons[1]
- gen(p, it, a)
- if a.typ == etyBaseIndex:
- addf(r.res, "Field$#: [$#, $#]", [i.rope, a.address, a.res])
- else:
- addf(r.res, "Field$#: $#", [i.rope, a.res])
- r.res.add("}")
- proc genObjConstr(p: PProc, n: PNode, r: var TCompRes) =
- var a: TCompRes
- r.kind = resExpr
- var initList : Rope
- var fieldIDs = initIntSet()
- for i in countup(1, sonsLen(n) - 1):
- if i > 1: add(initList, ", ")
- var it = n.sons[i]
- internalAssert p.config, it.kind == nkExprColonExpr
- let val = it.sons[1]
- gen(p, val, a)
- var f = it.sons[0].sym
- if f.loc.r == nil: f.loc.r = mangleName(p.module, f)
- fieldIDs.incl(f.id)
- let typ = val.typ.skipTypes(abstractInst)
- if (typ.kind in IntegralTypes+{tyCstring, tyRef, tyPtr} and
- mapType(p, typ) != etyBaseIndex) or
- a.typ == etyBaseIndex or
- needsNoCopy(p, it.sons[1]):
- discard
- else:
- useMagic(p, "nimCopy")
- a.res = "nimCopy(null, $1, $2)" % [a.rdLoc, genTypeInfo(p, typ)]
- if a.typ == etyBaseIndex:
- addf(initList, "$#: [$#, $#]", [f.loc.r, a.address, a.res])
- else:
- addf(initList, "$#: $#", [f.loc.r, a.res])
- let t = skipTypes(n.typ, abstractInst + skipPtrs)
- createObjInitList(p, t, fieldIDs, initList)
- r.res = ("{$1}") % [initList]
- proc genConv(p: PProc, n: PNode, r: var TCompRes) =
- var dest = skipTypes(n.typ, abstractVarRange)
- var src = skipTypes(n.sons[1].typ, abstractVarRange)
- gen(p, n.sons[1], r)
- if dest.kind == src.kind:
- # no-op conversion
- return
- case dest.kind:
- of tyBool:
- r.res = "(!!($1))" % [r.res]
- r.kind = resExpr
- of tyInt:
- r.res = "(($1)|0)" % [r.res]
- else:
- # TODO: What types must we handle here?
- discard
- proc upConv(p: PProc, n: PNode, r: var TCompRes) =
- gen(p, n.sons[0], r) # XXX
- proc genRangeChck(p: PProc, n: PNode, r: var TCompRes, magic: string) =
- var a, b: TCompRes
- gen(p, n.sons[0], r)
- if optRangeCheck in p.options:
- gen(p, n.sons[1], a)
- gen(p, n.sons[2], b)
- useMagic(p, "chckRange")
- r.res = "chckRange($1, $2, $3)" % [r.res, a.res, b.res]
- r.kind = resExpr
- proc convStrToCStr(p: PProc, n: PNode, r: var TCompRes) =
- # we do an optimization here as this is likely to slow down
- # much of the code otherwise:
- if n.sons[0].kind == nkCStringToString:
- gen(p, n.sons[0].sons[0], r)
- else:
- gen(p, n.sons[0], r)
- if r.res == nil: internalError(p.config, n.info, "convStrToCStr")
- useMagic(p, "toJSStr")
- r.res = "toJSStr($1)" % [r.res]
- r.kind = resExpr
- proc convCStrToStr(p: PProc, n: PNode, r: var TCompRes) =
- # we do an optimization here as this is likely to slow down
- # much of the code otherwise:
- if n.sons[0].kind == nkStringToCString:
- gen(p, n.sons[0].sons[0], r)
- else:
- gen(p, n.sons[0], r)
- if r.res == nil: internalError(p.config, n.info, "convCStrToStr")
- useMagic(p, "cstrToNimstr")
- r.res = "cstrToNimstr($1)" % [r.res]
- r.kind = resExpr
- proc genReturnStmt(p: PProc, n: PNode) =
- if p.procDef == nil: internalError(p.config, n.info, "genReturnStmt")
- p.beforeRetNeeded = true
- if n.sons[0].kind != nkEmpty:
- genStmt(p, n.sons[0])
- else:
- genLineDir(p, n)
- lineF(p, "break BeforeRet;$n", [])
- proc frameCreate(p: PProc; procname, filename: Rope): Rope =
- let frameFmt =
- "var F={procname:$1,prev:framePtr,filename:$2,line:0};$n"
- result = p.indentLine(frameFmt % [procname, filename])
- result.add p.indentLine(ropes.`%`("framePtr = F;$n", []))
- proc frameDestroy(p: PProc): Rope =
- result = p.indentLine rope(("framePtr = F.prev;") & "\L")
- proc genProcBody(p: PProc, prc: PSym): Rope =
- if hasFrameInfo(p):
- result = frameCreate(p,
- makeJSString(prc.owner.name.s & '.' & prc.name.s),
- makeJSString(toFilename(p.config, prc.info)))
- else:
- result = nil
- if p.beforeRetNeeded:
- result.add p.indentLine(~"BeforeRet: do {$n")
- result.add p.body
- result.add p.indentLine(~"} while (false);$n")
- else:
- add(result, p.body)
- if prc.typ.callConv == ccSysCall:
- result = ("try {$n$1} catch (e) {$n" &
- " alert(\"Unhandled exception:\\n\" + e.message + \"\\n\"$n}") % [result]
- if hasFrameInfo(p):
- add(result, frameDestroy(p))
- proc optionaLine(p: Rope): Rope =
- if p == nil:
- return nil
- else:
- return p & "\L"
- proc genProc(oldProc: PProc, prc: PSym): Rope =
- var
- resultSym: PSym
- a: TCompRes
- #if gVerbosity >= 3:
- # echo "BEGIN generating code for: " & prc.name.s
- var p = newProc(oldProc.g, oldProc.module, prc.ast, prc.options)
- p.up = oldProc
- var returnStmt: Rope = nil
- var resultAsgn: Rope = nil
- var name = mangleName(p.module, prc)
- let header = generateHeader(p, prc.typ)
- if prc.typ.sons[0] != nil and sfPure notin prc.flags:
- resultSym = prc.ast.sons[resultPos].sym
- let mname = mangleName(p.module, resultSym)
- if not isindirect(resultSym) and
- resultSym.typ.kind in {tyVar, tyPtr, tyLent, tyRef} and
- mapType(p, resultSym.typ) == etyBaseIndex:
- resultAsgn = p.indentLine(("var $# = null;$n") % [mname])
- resultAsgn.add p.indentLine("var $#_Idx = 0;$n" % [mname])
- else:
- let resVar = createVar(p, resultSym.typ, isIndirect(resultSym))
- resultAsgn = p.indentLine(("var $# = $#;$n") % [mname, resVar])
- gen(p, prc.ast.sons[resultPos], a)
- if mapType(p, resultSym.typ) == etyBaseIndex:
- returnStmt = "return [$#, $#];$n" % [a.address, a.res]
- else:
- returnStmt = "return $#;$n" % [a.res]
- let transformed_body = transformBody(oldProc.module.graph, prc, cache = false)
- p.nested: genStmt(p, transformed_body)
- var def: Rope
- if not prc.constraint.isNil:
- def = (prc.constraint.strVal & " {$n$#$#$#$#$#") %
- [ returnType,
- name,
- header,
- optionaLine(p.globals),
- optionaLine(p.locals),
- optionaLine(resultAsgn),
- optionaLine(genProcBody(p, prc)),
- optionaLine(p.indentLine(returnStmt))]
- else:
- result = ~"\L"
- if optHotCodeReloading in p.config.options:
- # Here, we introduce thunks that create the equivalent of a jump table
- # for all global functions, because references to them may be stored
- # in JavaScript variables. The added indirection ensures that such
- # references will end up calling the reloaded code.
- var thunkName = name
- name = name & "IMLP"
- result.add("function $#() { return $#.apply(this, arguments); }$n" %
- [thunkName, name])
- def = "function $#($#) {$n$#$#$#$#$#" %
- [ name,
- header,
- optionaLine(p.globals),
- optionaLine(p.locals),
- optionaLine(resultAsgn),
- optionaLine(genProcBody(p, prc)),
- optionaLine(p.indentLine(returnStmt))]
- dec p.extraIndent
- result.add p.indentLine(def)
- result.add p.indentLine(~"}$n")
- #if gVerbosity >= 3:
- # echo "END generated code for: " & prc.name.s
- proc genStmt(p: PProc, n: PNode) =
- var r: TCompRes
- gen(p, n, r)
- if r.res != nil: lineF(p, "$#;$n", [r.res])
- proc genPragma(p: PProc, n: PNode) =
- for it in n.sons:
- case whichPragma(it)
- of wEmit: genAsmOrEmitStmt(p, it.sons[1])
- else: discard
- proc genCast(p: PProc, n: PNode, r: var TCompRes) =
- var dest = skipTypes(n.typ, abstractVarRange)
- var src = skipTypes(n.sons[1].typ, abstractVarRange)
- gen(p, n.sons[1], r)
- if dest.kind == src.kind:
- # no-op conversion
- return
- let toInt = (dest.kind in tyInt..tyInt32)
- let toUint = (dest.kind in tyUInt..tyUInt32)
- let fromInt = (src.kind in tyInt..tyInt32)
- let fromUint = (src.kind in tyUInt..tyUInt32)
- if toUint and (fromInt or fromUint):
- let trimmer = unsignedTrimmer(dest.size)
- r.res = "($1 $2)" % [r.res, trimmer]
- elif toInt:
- if fromInt:
- let trimmer = unsignedTrimmer(dest.size)
- r.res = "($1 $2)" % [r.res, trimmer]
- elif fromUint:
- if src.size == 4 and dest.size == 4:
- # XXX prevent multi evaluations
- r.res = "($1|0)" % [r.res]
- else:
- let trimmer = unsignedTrimmer(dest.size)
- let minuend = case dest.size
- of 1: "0xfe"
- of 2: "0xfffe"
- of 4: "0xfffffffe"
- else: ""
- r.res = "($1 - ($2 $3))" % [rope minuend, r.res, trimmer]
- elif (src.kind == tyPtr and mapType(p, src) == etyObject) and dest.kind == tyPointer:
- r.address = r.res
- r.res = ~"null"
- r.typ = etyBaseIndex
- elif (dest.kind == tyPtr and mapType(p, dest) == etyObject) and src.kind == tyPointer:
- r.res = r.address
- r.typ = etyObject
- proc gen(p: PProc, n: PNode, r: var TCompRes) =
- r.typ = etyNone
- if r.kind != resCallee: r.kind = resNone
- #r.address = nil
- r.res = nil
- case n.kind
- of nkSym:
- genSym(p, n, r)
- of nkCharLit..nkUInt64Lit:
- if n.typ.kind == tyBool:
- r.res = if n.intVal == 0: rope"false" else: rope"true"
- else:
- r.res = rope(n.intVal)
- r.kind = resExpr
- of nkNilLit:
- if isEmptyType(n.typ):
- discard
- elif mapType(p, n.typ) == etyBaseIndex:
- r.typ = etyBaseIndex
- r.address = rope"null"
- r.res = rope"0"
- r.kind = resExpr
- else:
- r.res = rope"null"
- r.kind = resExpr
- of nkStrLit..nkTripleStrLit:
- if skipTypes(n.typ, abstractVarRange).kind == tyString:
- if n.strVal.len != 0:
- useMagic(p, "makeNimstrLit")
- r.res = "makeNimstrLit($1)" % [makeJSString(n.strVal)]
- else:
- r.res = rope"[]"
- else:
- r.res = makeJSString(n.strVal, false)
- r.kind = resExpr
- of nkFloatLit..nkFloat64Lit:
- let f = n.floatVal
- case classify(f)
- of fcNaN:
- r.res = rope"NaN"
- of fcNegZero:
- r.res = rope"-0.0"
- of fcZero:
- r.res = rope"0.0"
- of fcInf:
- r.res = rope"Infinity"
- of fcNegInf:
- r.res = rope"-Infinity"
- else: r.res = rope(f.toStrMaxPrecision)
- r.kind = resExpr
- of nkCallKinds:
- if isEmptyType(n.typ): genLineDir(p, n)
- if (n.sons[0].kind == nkSym) and (n.sons[0].sym.magic != mNone):
- genMagic(p, n, r)
- elif n.sons[0].kind == nkSym and sfInfixCall in n.sons[0].sym.flags and
- n.len >= 1:
- genInfixCall(p, n, r)
- else:
- genCall(p, n, r)
- of nkClosure: gen(p, n[0], r)
- of nkCurly: genSetConstr(p, n, r)
- of nkBracket: genArrayConstr(p, n, r)
- of nkPar, nkTupleConstr: genTupleConstr(p, n, r)
- of nkObjConstr: genObjConstr(p, n, r)
- of nkHiddenStdConv, nkHiddenSubConv, nkConv: genConv(p, n, r)
- of nkAddr, nkHiddenAddr:
- genAddr(p, n, r)
- of nkDerefExpr, nkHiddenDeref: genDeref(p, n, r)
- of nkBracketExpr: genArrayAccess(p, n, r)
- of nkDotExpr: genFieldAccess(p, n, r)
- of nkCheckedFieldExpr: genCheckedFieldOp(p, n, nil, r)
- of nkObjDownConv: gen(p, n.sons[0], r)
- of nkObjUpConv: upConv(p, n, r)
- of nkCast: genCast(p, n, r)
- of nkChckRangeF: genRangeChck(p, n, r, "chckRangeF")
- of nkChckRange64: genRangeChck(p, n, r, "chckRange64")
- of nkChckRange: genRangeChck(p, n, r, "chckRange")
- of nkStringToCString: convStrToCStr(p, n, r)
- of nkCStringToString: convCStrToStr(p, n, r)
- of nkEmpty: discard
- of nkLambdaKinds:
- let s = n.sons[namePos].sym
- discard mangleName(p.module, s)
- r.res = s.loc.r
- if lfNoDecl in s.loc.flags or s.magic != mNone: discard
- elif not p.g.generatedSyms.containsOrIncl(s.id):
- add(p.locals, genProc(p, s))
- of nkType: r.res = genTypeInfo(p, n.typ)
- of nkStmtList, nkStmtListExpr:
- # this shows the distinction is nice for backends and should be kept
- # in the frontend
- let isExpr = not isEmptyType(n.typ)
- for i in countup(0, sonsLen(n) - 1 - isExpr.ord):
- genStmt(p, n.sons[i])
- if isExpr:
- gen(p, lastSon(n), r)
- of nkBlockStmt, nkBlockExpr: genBlock(p, n, r)
- of nkIfStmt, nkIfExpr: genIf(p, n, r)
- of nkWhen:
- # This is "when nimvm" node
- gen(p, n.sons[1].sons[0], r)
- of nkWhileStmt: genWhileStmt(p, n)
- of nkVarSection, nkLetSection: genVarStmt(p, n)
- of nkConstSection: discard
- of nkForStmt, nkParForStmt:
- internalError(p.config, n.info, "for statement not eliminated")
- of nkCaseStmt: genCaseJS(p, n, r)
- of nkReturnStmt: genReturnStmt(p, n)
- of nkBreakStmt: genBreakStmt(p, n)
- of nkAsgn: genAsgn(p, n)
- of nkFastAsgn: genFastAsgn(p, n)
- of nkDiscardStmt:
- if n.sons[0].kind != nkEmpty:
- genLineDir(p, n)
- gen(p, n.sons[0], r)
- of nkAsmStmt: genAsmOrEmitStmt(p, n)
- of nkTryStmt: genTry(p, n, r)
- of nkRaiseStmt: genRaiseStmt(p, n)
- of nkTypeSection, nkCommentStmt, nkIteratorDef, nkIncludeStmt,
- nkImportStmt, nkImportExceptStmt, nkExportStmt, nkExportExceptStmt,
- nkFromStmt, nkTemplateDef, nkMacroDef, nkStaticStmt: discard
- of nkPragma: genPragma(p, n)
- of nkProcDef, nkFuncDef, nkMethodDef, nkConverterDef:
- var s = n.sons[namePos].sym
- if {sfExportc, sfCompilerProc} * s.flags == {sfExportc}:
- genSym(p, n.sons[namePos], r)
- r.res = nil
- of nkGotoState, nkState:
- internalError(p.config, n.info, "first class iterators not implemented")
- of nkPragmaBlock: gen(p, n.lastSon, r)
- of nkComesFrom:
- discard "XXX to implement for better stack traces"
- else: internalError(p.config, n.info, "gen: unknown node type: " & $n.kind)
- proc newModule(g: ModuleGraph; module: PSym): BModule =
- new(result)
- result.module = module
- result.sigConflicts = initCountTable[SigHash]()
- if g.backend == nil:
- g.backend = newGlobals()
- result.graph = g
- result.config = g.config
- proc genHeader(): Rope =
- result = (
- "/* Generated by the Nim Compiler v$1 */$n" &
- "/* (c) " & copyrightYear & " Andreas Rumpf */$n$n" &
- "var framePtr = null;$n" &
- "var excHandler = 0;$n" &
- "var lastJSError = null;$n" &
- "if (typeof Int8Array === 'undefined') Int8Array = Array;$n" &
- "if (typeof Int16Array === 'undefined') Int16Array = Array;$n" &
- "if (typeof Int32Array === 'undefined') Int32Array = Array;$n" &
- "if (typeof Uint8Array === 'undefined') Uint8Array = Array;$n" &
- "if (typeof Uint16Array === 'undefined') Uint16Array = Array;$n" &
- "if (typeof Uint32Array === 'undefined') Uint32Array = Array;$n" &
- "if (typeof Float32Array === 'undefined') Float32Array = Array;$n" &
- "if (typeof Float64Array === 'undefined') Float64Array = Array;$n") %
- [rope(VersionAsString)]
- proc genModule(p: PProc, n: PNode) =
- if optStackTrace in p.options:
- add(p.body, frameCreate(p,
- makeJSString("module " & p.module.module.name.s),
- makeJSString(toFilename(p.config, p.module.module.info))))
- let n_transformed = transformStmt(p.module.graph, p.module.module, n)
- genStmt(p, n_transformed)
- if optStackTrace in p.options:
- add(p.body, frameDestroy(p))
- proc myProcess(b: PPassContext, n: PNode): PNode =
- result = n
- let m = BModule(b)
- if passes.skipCodegen(m.config, n): return n
- if m.module == nil: internalError(m.config, n.info, "myProcess")
- let globals = PGlobals(m.graph.backend)
- var p = newProc(globals, m, nil, m.module.options)
- p.unique = globals.unique
- genModule(p, n)
- add(p.g.code, p.locals)
- add(p.g.code, p.body)
- proc wholeCode(graph: ModuleGraph; m: BModule): Rope =
- let globals = PGlobals(graph.backend)
- for prc in globals.forwarded:
- if not globals.generatedSyms.containsOrIncl(prc.id):
- var p = newProc(globals, m, nil, m.module.options)
- attachProc(p, prc)
- var disp = generateMethodDispatchers(graph)
- for i in 0..sonsLen(disp)-1:
- let prc = disp.sons[i].sym
- if not globals.generatedSyms.containsOrIncl(prc.id):
- var p = newProc(globals, m, nil, m.module.options)
- attachProc(p, prc)
- result = globals.typeInfo & globals.constants & globals.code
- proc getClassName(t: PType): Rope =
- var s = t.sym
- if s.isNil or sfAnon in s.flags:
- s = skipTypes(t, abstractPtrs).sym
- if s.isNil or sfAnon in s.flags:
- doAssert(false, "cannot retrieve class name")
- if s.loc.r != nil: result = s.loc.r
- else: result = rope(s.name.s)
- proc genClass(conf: ConfigRef; obj: PType; content: Rope; ext: string) =
- let cls = getClassName(obj)
- let t = skipTypes(obj, abstractPtrs)
- let extends = if t.kind == tyObject and t.sons[0] != nil:
- " extends " & getClassName(t.sons[0])
- else: nil
- let result = ("<?php$n" &
- "/* Generated by the Nim Compiler v$# */$n" &
- "/* (c) " & copyrightYear & " Andreas Rumpf */$n$n" &
- "require_once \"nimsystem.php\";$n" &
- "class $#$# {$n$#$n}$n") %
- [rope(VersionAsString), cls, extends, content]
- let outfile = changeFileExt(completeCFilePath(conf, AbsoluteFile($cls)), ext)
- discard writeRopeIfNotEqual(result, outfile)
- proc myClose(graph: ModuleGraph; b: PPassContext, n: PNode): PNode =
- result = myProcess(b, n)
- var m = BModule(b)
- if passes.skipCodegen(m.config, n): return n
- if sfMainModule in m.module.flags:
- let globals = PGlobals(graph.backend)
- let ext = "js"
- let f = if globals.classes.len == 0: toFilename(m.config, FileIndex m.module.position)
- else: "nimsystem"
- let code = wholeCode(graph, m)
- let outfile =
- if not m.config.outFile.isEmpty:
- if m.config.outFile.string.isAbsolute: m.config.outFile
- else: AbsoluteFile(getCurrentDir() / m.config.outFile.string)
- else:
- changeFileExt(completeCFilePath(m.config, AbsoluteFile f), ext)
- let (outDir, _, _) = splitFile(outfile)
- if not outDir.isEmpty:
- createDir(outDir)
- discard writeRopeIfNotEqual(genHeader() & code, outfile)
- for obj, content in items(globals.classes):
- genClass(m.config, obj, content, ext)
- proc myOpen(graph: ModuleGraph; s: PSym): PPassContext =
- result = newModule(graph, s)
- const JSgenPass* = makePass(myOpen, myProcess, myClose)
|