summary refs log tree commit diff stats
path: root/compiler/service.nim
blob: 7cb9d7d297b4e54ff1177077900a89cbe3b755ee (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#
#
#           The Nim Compiler
#        (c) Copyright 2015 Andreas Rumpf
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

## Implements the "compiler as a service" feature.

import
  times, commands, options, msgs, nimconf,
  extccomp, strutils, os, platform, parseopt

when useCaas:
  import net

# We cache modules and the dependency graph. However, we don't check for
# file changes but expect the client to tell us about them, otherwise the
# repeated CRC calculations may turn out to be too slow.

var
  curCaasCmd* = ""
  lastCaasCmd* = ""
    # in caas mode, the list of defines and options will be given at start-up?
    # it's enough to check that the previous compilation command is the same?

proc processCmdLine*(pass: TCmdLinePass, cmd: string) =
  var p = parseopt.initOptParser(cmd)
  var argsCount = 0
  while true:
    parseopt.next(p)
    case p.kind
    of cmdEnd: break
    of cmdLongoption, cmdShortOption:
      if p.key == " ":
        p.key = "-"
        if processArgument(pass, p, argsCount): break
      else:
        processSwitch(pass, p)
    of cmdArgument:
      if processArgument(pass, p, argsCount): break
  if pass == passCmd2:
    if optRun notin gGlobalOptions and arguments != "" and options.command.normalize != "run":
      rawMessage(errArgsNeedRunOption, [])

proc serve*(action: proc (){.nimcall.}) =
  template execute(cmd) =
    curCaasCmd = cmd
    processCmdLine(passCmd2, cmd)
    action()
    gErrorCounter = 0

  let typ = getConfigVar("server.type")
  case typ
  of "stdin":
    while true:
      var line = stdin.readLine.string
      if line == "quit": quit()
      execute line
      echo ""
      flushFile(stdout)

  of "tcp", "":
    when useCaas:
      var server = newSocket()
      let p = getConfigVar("server.port")
      let port = if p.len > 0: parseInt(p).Port else: 6000.Port
      server.bindAddr(port, getConfigVar("server.address"))
      var inp = "".TaintedString
      server.listen()
      var stdoutSocket = newSocket()
      msgs.writelnHook = proc (line: string) =
        stdoutSocket.send(line & "\c\L")

      while true:
        accept(server, stdoutSocket)
        stdoutSocket.readLine(inp)
        execute inp.string
        stdoutSocket.send("\c\L")
        stdoutSocket.close()
    else:
      msgQuit "server.type not supported; compiler built without caas support"
  else:
    echo "Invalid server.type:", typ
    msgQuit 1
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494
#
#
#           The Nimrod Compiler
#        (c) Copyright 2012 Andreas Rumpf
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

# This is the EMCAScript (also known as JavaScript) code generator.
# **Invariant: each expression occurs only once in the generated
# code!**

import 
  ast, astalgo, strutils, hashes, trees, platform, magicsys, extccomp,
  options, nversion, nimsets, msgs, crc, bitsets, idents, lists, types, os,
  times, ropes, math, passes, ccgutils, wordrecg, renderer, rodread, rodutils,
  intsets, cgmeth

proc ecmasgenPass*(): TPass
# implementation

type 
  TEcmasGen = object of TPassContext
    filename*: string
    module*: PSym

  BModule = ref TEcmasGen
  TEcmasTypeKind = enum 
    etyNone,                  # no type
    etyNull,                  # null type
    etyProc,                  # proc type
    etyBool,                  # bool type
    etyInt,                   # Ecmascript's int
    etyFloat,                 # Ecmascript's float
    etyString,                # Ecmascript's string
    etyObject,                # Ecmascript's reference to an object
    etyBaseIndex              # base + index needed
  TCompRes{.final.} = object 
    kind*: TEcmasTypeKind
    com*: PRope               # computation part
                              # address if this is a (address, index)-tuple
    res*: PRope               # result part; index if this is an
                              # (address, index)-tuple
  
  TBlock{.final.} = object 
    id*: int                  # the ID of the label; positive means that it
                              # has been used (i.e. the label should be emitted)
    nestedTryStmts*: int      # how many try statements is it nested into
  
  TGlobals{.final.} = object 
    typeInfo*, code*: PRope
    typeInfoGenerated*: TIntSet

  PGlobals = ref TGlobals
  TProc{.final.} = object 
    procDef*: PNode
    prc*: PSym
    data*: PRope
    options*: TOptions
    module*: BModule
    globals*: PGlobals
    BeforeRetNeeded*: bool
    nestedTryStmts*: int
    unique*: int
    blocks*: seq[TBlock]


proc newGlobals(): PGlobals = 
  new(result)
  result.typeInfoGenerated = initIntSet()

proc initCompRes(r: var TCompRes) = 
  r.com = nil
  r.res = nil
  r.kind = etyNone

proc initProc(p: var TProc, globals: PGlobals, module: BModule, procDef: PNode, 
              options: TOptions) = 
  p.blocks = @[]
  p.options = options
  p.module = module
  p.procDef = procDef
  p.globals = globals
  if procDef != nil: p.prc = procDef.sons[namePos].sym
  
const 
  MappedToObject = {tyObject, tyArray, tyArrayConstr, tyTuple, tyOpenArray, 
    tySet, tyVar, tyRef, tyPtr, tyBigNum}

proc mapType(typ: PType): TEcmasTypeKind = 
  var t = skipTypes(typ, abstractInst)
  case t.kind
  of tyVar, tyRef, tyPtr: 
    if skipTypes(t.sons[0], abstractInst).kind in mappedToObject: 
      result = etyObject
    else: 
      result = etyBaseIndex
  of tyPointer: 
    # treat a tyPointer like a typed pointer to an array of bytes
    result = etyInt
  of tyRange, tyDistinct, tyOrdinal, tyConst, tyMutable, tyIter, tyVarargs,
     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: result = etyInt # little hack to get right semantics
  of tyObject, tyArray, tyArrayConstr, tyTuple, tyOpenArray, tyBigNum: 
    result = etyObject
  of tyNil: result = etyNull
  of tyGenericInst, tyGenericParam, tyGenericBody, tyGenericInvokation, tyNone, 
     tyForward, tyEmpty, tyExpr, tyStmt, tyTypeDesc, tyTypeClass: 
    result = etyNone
  of tyProc: result = etyProc
  of tyCString: result = etyString
  
proc mangle(name: string): string = 
  result = ""
  for i in countup(0, len(name) - 1): 
    case name[i]
    of 'A'..'Z': 
      add(result, chr(ord(name[i]) - ord('A') + ord('a')))
    of '_': 
      nil
    of 'a'..'z', '0'..'9': 
      add(result, name[i])
    else: add(result, 'X' & toHex(ord(name[i]), 2))
  
proc mangleName(s: PSym): PRope = 
  result = s.loc.r
  if result == nil: 
    result = toRope(mangle(s.name.s))
    app(result, "_")
    app(result, toRope(s.id))
    s.loc.r = result

proc genTypeInfo(p: var TProc, typ: PType): PRope
proc genObjectFields(p: var TProc, typ: PType, n: PNode): PRope = 
  var 
    s, u: PRope
    length: int
    field: PSym
    b: PNode
  result = nil
  case n.kind
  of nkRecList: 
    length = sonsLen(n)
    if length == 1: 
      result = genObjectFields(p, typ, n.sons[0])
    else: 
      s = nil
      for i in countup(0, length - 1): 
        if i > 0: app(s, ", " & tnl)
        app(s, genObjectFields(p, typ, n.sons[i]))
      result = ropef("{kind: 2, len: $1, offset: 0, " &
          "typ: null, name: null, sons: [$2]}", [toRope(length), s])
  of nkSym: 
    field = n.sym
    s = genTypeInfo(p, field.typ)
    result = ropef("{kind: 1, offset: \"$1\", len: 0, " &
        "typ: $2, name: $3, sons: null}", 
                   [mangleName(field), s, makeCString(field.name.s)])
  of nkRecCase: 
    length = sonsLen(n)
    if (n.sons[0].kind != nkSym): InternalError(n.info, "genObjectFields")
    field = n.sons[0].sym
    s = genTypeInfo(p, field.typ)
    for i in countup(1, length - 1): 
      b = n.sons[i]           # branch
      u = nil
      case b.kind
      of nkOfBranch: 
        if sonsLen(b) < 2: 
          internalError(b.info, "genObjectFields; nkOfBranch broken")
        for j in countup(0, sonsLen(b) - 2): 
          if u != nil: app(u, ", ")
          if b.sons[j].kind == nkRange: 
            appf(u, "[$1, $2]", [toRope(getOrdValue(b.sons[j].sons[0])), 
                                 toRope(getOrdValue(b.sons[j].sons[1]))])
          else: 
            app(u, toRope(getOrdValue(b.sons[j])))
      of nkElse: 
        u = toRope(lengthOrd(field.typ))
      else: internalError(n.info, "genObjectFields(nkRecCase)")
      if result != nil: app(result, ", " & tnl)
      appf(result, "[SetConstr($1), $2]", 
           [u, genObjectFields(p, typ, lastSon(b))])
    result = ropef("{kind: 3, offset: \"$1\", len: $3, " &
        "typ: $2, name: $4, sons: [$5]}", [mangleName(field), s, 
        toRope(lengthOrd(field.typ)), makeCString(field.name.s), result])
  else: internalError(n.info, "genObjectFields")
  
proc genObjectInfo(p: var TProc, typ: PType, name: PRope) = 
  var s = ropef("var $1 = {size: 0, kind: $2, base: null, node: null, " &
                "finalizer: null};$n", [name, toRope(ord(typ.kind))])
  prepend(p.globals.typeInfo, s)
  appf(p.globals.typeInfo, "var NNI$1 = $2;$n", 
       [toRope(typ.id), genObjectFields(p, typ, typ.n)])
  appf(p.globals.typeInfo, "$1.node = NNI$2;$n", [name, toRope(typ.id)])
  if (typ.kind == tyObject) and (typ.sons[0] != nil): 
    appf(p.globals.typeInfo, "$1.base = $2;$n", 
         [name, genTypeInfo(p, typ.sons[0])])

proc genEnumInfo(p: var TProc, typ: PType, name: PRope) = 
  var 
    s, n: PRope
    length: int
    field: PSym
  length = sonsLen(typ.n)
  s = nil
  for i in countup(0, length - 1): 
    if (typ.n.sons[i].kind != nkSym): InternalError(typ.n.info, "genEnumInfo")
    field = typ.n.sons[i].sym
    if i > 0: app(s, ", " & tnl)
    appf(s, "{kind: 1, offset: $1, typ: $2, name: $3, len: 0, sons: null}", 
         [toRope(field.position), name, makeCString(field.name.s)])
  n = ropef("var NNI$1 = {kind: 2, offset: 0, typ: null, " &
      "name: null, len: $2, sons: [$3]};$n", [toRope(typ.id), toRope(length), s])
  s = ropef("var $1 = {size: 0, kind: $2, base: null, node: null, " &
      "finalizer: null};$n", [name, toRope(ord(typ.kind))])
  prepend(p.globals.typeInfo, s)
  app(p.globals.typeInfo, n)
  appf(p.globals.typeInfo, "$1.node = NNI$2;$n", [name, toRope(typ.id)])
  if typ.sons[0] != nil: 
    appf(p.globals.typeInfo, "$1.base = $2;$n", 
         [name, genTypeInfo(p, typ.sons[0])])

proc genTypeInfo(p: var TProc, typ: PType): PRope = 
  var t = typ
  if t.kind == tyGenericInst: t = lastSon(t)
  result = ropef("NTI$1", [toRope(t.id)])
  if ContainsOrIncl(p.globals.TypeInfoGenerated, t.id): return 
  case t.kind
  of tyDistinct: 
    result = genTypeInfo(p, typ.sons[0])
  of tyPointer, tyProc, tyBool, tyChar, tyCString, tyString, tyInt..tyFloat128: 
    var s = ropef(
      "var $1 = {size: 0,kind: $2,base: null,node: null,finalizer: null};$n", 
              [result, toRope(ord(t.kind))])
    prepend(p.globals.typeInfo, s)
  of tyVar, tyRef, tyPtr, tySequence, tyRange, tySet: 
    var s = ropef(
      "var $1 = {size: 0,kind: $2,base: null,node: null,finalizer: null};$n", 
              [result, toRope(ord(t.kind))])
    prepend(p.globals.typeInfo, s)
    appf(p.globals.typeInfo, "$1.base = $2;$n", 
         [result, genTypeInfo(p, typ.sons[0])])
  of tyArrayConstr, tyArray: 
    var s = ropef(
      "var $1 = {size: 0,kind: $2,base: null,node: null,finalizer: null};$n",
              [result, toRope(ord(t.kind))])
    prepend(p.globals.typeInfo, s)
    appf(p.globals.typeInfo, "$1.base = $2;$n", 
         [result, genTypeInfo(p, typ.sons[1])])
  of tyEnum: genEnumInfo(p, t, result)
  of tyObject, tyTuple: genObjectInfo(p, t, result)
  else: InternalError("genTypeInfo(" & $t.kind & ')')
  
proc gen(p: var TProc, n: PNode, r: var TCompRes)
proc genStmt(p: var TProc, n: PNode, r: var TCompRes)
proc useMagic(p: var TProc, ident: string) =
  nil 
  # to implement
  
proc mergeExpr(a, b: PRope): PRope = 
  if (a != nil): 
    if b != nil: result = ropef("($1, $2)", [a, b])
    else: result = a
  else: 
    result = b
  
proc mergeExpr(r: TCompRes): PRope = 
  result = mergeExpr(r.com, r.res)

proc mergeStmt(r: TCompRes): PRope = 
  if r.res == nil: result = r.com
  elif r.com == nil: result = r.res
  else: result = ropef("$1$2", [r.com, r.res])
  
proc genAnd(p: var TProc, a, b: PNode, r: var TCompRes) = 
  var x, y: TCompRes
  gen(p, a, x)
  gen(p, b, y)
  r.res = ropef("($1 && $2)", [mergeExpr(x), mergeExpr(y)])

proc genOr(p: var TProc, a, b: PNode, r: var TCompRes) = 
  var x, y: TCompRes
  gen(p, a, x)
  gen(p, b, y)
  r.res = ropef("($1 || $2)", [mergeExpr(x), mergeExpr(y)])

type 
  TMagicFrmt = array[0..3, string]

const # magic checked op; magic unchecked op; checked op; unchecked op
  ops: array[mAddi..mStrToStr, TMagicFrmt] = [
    ["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.floor($1 / $2)"], # DivI
    ["modInt", "", "modInt($1, $2)", "Math.floor($1 % $2)"], # ModI
    ["addInt64", "", "addInt64($1, $2)", "($1 + $2)"], # AddI64
    ["subInt64", "", "subInt64($1, $2)", "($1 - $2)"], # SubI64
    ["mulInt64", "", "mulInt64($1, $2)", "($1 * $2)"], # MulI64
    ["divInt64", "", "divInt64($1, $2)", "Math.floor($1 / $2)"], # DivI64
    ["modInt64", "", "modInt64($1, $2)", "Math.floor($1 % $2)"], # ModI64
    ["", "", "($1 + $2)", "($1 + $2)"], # AddF64
    ["", "", "($1 - $2)", "($1 - $2)"], # SubF64
    ["", "", "($1 * $2)", "($1 * $2)"], # MulF64
    ["", "", "($1 / $2)", "($1 / $2)"], # DivF64
    ["", "", "($1 >>> $2)", "($1 >>> $2)"], # ShrI
    ["", "", "($1 << $2)", "($1 << $2)"], # ShlI
    ["", "", "($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
    ["", "", "($1 >>> $2)", "($1 >>> $2)"], # ShrI64
    ["", "", "($1 << $2)", "($1 << $2)"], # ShlI64
    ["", "", "($1 & $2)", "($1 & $2)"], # BitandI64
    ["", "", "($1 | $2)", "($1 | $2)"], # BitorI64
    ["", "", "($1 ^ $2)", "($1 ^ $2)"], # BitxorI64
    ["nimMin", "nimMin", "nimMin($1, $2)", "nimMin($1, $2)"], # MinI64
    ["nimMax", "nimMax", "nimMax($1, $2)", "nimMax($1, $2)"], # MaxI64
    ["nimMin", "nimMin", "nimMin($1, $2)", "nimMin($1, $2)"], # MinF64
    ["nimMax", "nimMax", "nimMax($1, $2)", "nimMax($1, $2)"], # MaxF64
    ["AddU", "AddU", "AddU($1, $2)", "AddU($1, $2)"], # AddU
    ["SubU", "SubU", "SubU($1, $2)", "SubU($1, $2)"], # SubU
    ["MulU", "MulU", "MulU($1, $2)", "MulU($1, $2)"], # MulU
    ["DivU", "DivU", "DivU($1, $2)", "DivU($1, $2)"], # DivU
    ["ModU", "ModU", "ModU($1, $2)", "ModU($1, $2)"], # ModU
    ["AddU64", "AddU64", "AddU64($1, $2)", "AddU64($1, $2)"], # AddU64
    ["SubU64", "SubU64", "SubU64($1, $2)", "SubU64($1, $2)"], # SubU64
    ["MulU64", "MulU64", "MulU64($1, $2)", "MulU64($1, $2)"], # MulU64
    ["DivU64", "DivU64", "DivU64($1, $2)", "DivU64($1, $2)"], # DivU64
    ["ModU64", "ModU64", "ModU64($1, $2)", "ModU64($1, $2)"], # ModU64
    ["", "", "($1 == $2)", "($1 == $2)"], # EqI
    ["", "", "($1 <= $2)", "($1 <= $2)"], # LeI
    ["", "", "($1 < $2)", "($1 < $2)"], # LtI
    ["", "", "($1 == $2)", "($1 == $2)"], # EqI64
    ["", "", "($1 <= $2)", "($1 <= $2)"], # LeI64
    ["", "", "($1 < $2)", "($1 < $2)"], # LtI64
    ["", "", "($1 == $2)", "($1 == $2)"], # EqF64
    ["", "", "($1 <= $2)", "($1 <= $2)"], # LeF64
    ["", "", "($1 < $2)", "($1 < $2)"], # LtF64
    ["LeU", "LeU", "LeU($1, $2)", "LeU($1, $2)"], # LeU
    ["LtU", "LtU", "LtU($1, $2)", "LtU($1, $2)"], # LtU
    ["LeU64", "LeU64", "LeU64($1, $2)", "LeU64($1, $2)"], # LeU64
    ["LtU64", "LtU64", "LtU64($1, $2)", "LtU64($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)"], # EqProc
    ["", "", "($1 == $2)", "($1 == $2)"], # EqUntracedRef
    ["", "", "($1 <= $2)", "($1 <= $2)"], # LePtr
    ["", "", "($1 < $2)", "($1 < $2)"], # LtPtr
    ["", "", "($1 == $2)", "($1 == $2)"], # EqCString
    ["", "", "($1 != $2)", "($1 != $2)"], # Xor
    ["NegInt", "", "NegInt($1)", "-($1)"], # UnaryMinusI
    ["NegInt64", "", "NegInt64($1)", "-($1)"], # UnaryMinusI64
    ["AbsInt", "", "AbsInt($1)", "Math.abs($1)"], # AbsI
    ["AbsInt64", "", "AbsInt64($1)", "Math.abs($1)"], # AbsI64
    ["", "", "!($1)", "!($1)"], # Not
    ["", "", "+($1)", "+($1)"], # UnaryPlusI
    ["", "", "~($1)", "~($1)"], # BitnotI
    ["", "", "+($1)", "+($1)"], # UnaryPlusI64
    ["", "", "~($1)", "~($1)"], # BitnotI64
    ["", "", "+($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.floor($1)", "Math.floor($1)"], # ToInt
    ["", "", "Math.floor($1)", "Math.floor($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 binaryExpr(p: var TProc, n: PNode, r: var TCompRes, magic, frmt: string) = 
  var x, y: TCompRes
  if magic != "": useMagic(p, magic)
  gen(p, n.sons[1], x)
  gen(p, n.sons[2], y)
  r.res = ropef(frmt, [x.res, y.res])
  r.com = mergeExpr(x.com, y.com)

proc binaryStmt(p: var TProc, n: PNode, r: var TCompRes, magic, frmt: string) = 
  var x, y: TCompRes
  if magic != "": useMagic(p, magic)
  gen(p, n.sons[1], x)
  gen(p, n.sons[2], y)
  if x.com != nil: appf(r.com, "$1;$n", [x.com])
  if y.com != nil: appf(r.com, "$1;$n", [y.com])
  appf(r.com, frmt, [x.res, y.res])

proc unaryExpr(p: var TProc, n: PNode, r: var TCompRes, magic, frmt: string) = 
  if magic != "": useMagic(p, magic)
  gen(p, n.sons[1], r)
  r.res = ropef(frmt, [r.res])

proc arith(p: var TProc, n: PNode, r: var TCompRes, op: TMagic) = 
  var 
    x, y: TCompRes
    i: int
  if optOverflowCheck in p.options: i = 0
  else: i = 1
  useMagic(p, ops[op][i])
  if sonsLen(n) > 2: 
    gen(p, n.sons[1], x)
    gen(p, n.sons[2], y)
    r.res = ropef(ops[op][i + 2], [x.res, y.res])
    r.com = mergeExpr(x.com, y.com)
  else: 
    gen(p, n.sons[1], r)
    r.res = ropef(ops[op][i + 2], [r.res])

proc genLineDir(p: var TProc, n: PNode, r: var TCompRes) = 
  var line: int
  line = toLinenumber(n.info)
  if optLineDir in p.Options: 
    appf(r.com, "// line $2 \"$1\"$n", 
         [toRope(toFilename(n.info)), toRope(line)])
  if ({optStackTrace, optEndb} * p.Options == {optStackTrace, optEndb}) and
      ((p.prc == nil) or not (sfPure in p.prc.flags)): 
    useMagic(p, "endb")
    appf(r.com, "endb($1);$n", [toRope(line)])
  elif ({optLineTrace, optStackTrace} * p.Options ==
      {optLineTrace, optStackTrace}) and
      ((p.prc == nil) or not (sfPure in p.prc.flags)): 
    appf(r.com, "F.line = $1;$n", [toRope(line)])
  
proc finishTryStmt(p: var TProc, r: var TCompRes, howMany: int) = 
  for i in countup(1, howMany):
    app(r.com, "excHandler = excHandler.prev;" & tnl)
  
proc genWhileStmt(p: var TProc, n: PNode, r: var TCompRes) = 
  var 
    cond, stmt: TCompRes
    length, labl: int
  genLineDir(p, n, r)
  inc(p.unique)
  length = len(p.blocks)
  setlen(p.blocks, length + 1)
  p.blocks[length].id = - p.unique
  p.blocks[length].nestedTryStmts = p.nestedTryStmts
  labl = p.unique
  gen(p, n.sons[0], cond)
  genStmt(p, n.sons[1], stmt)
  if p.blocks[length].id > 0: 
    appf(r.com, "L$3: while ($1) {$n$2}$n", 
         [mergeExpr(cond), mergeStmt(stmt), toRope(labl)])
  else: 
    appf(r.com, "while ($1) {$n$2}$n", [mergeExpr(cond), mergeStmt(stmt)])
  setlen(p.blocks, length)

proc genTryStmt(p: var TProc, n: PNode, r: var TCompRes) = 
  # code to generate:
  #
  #  var sp = {prev: excHandler, exc: null};
  #  excHandler = sp;
  #  try {
  #    stmts;
  #  } catch (e) {
  #    if (e.typ && e.typ == NTI433 || e.typ == NTI2321) {
  #      stmts;
  #    } else if (e.typ && e.typ == NTI32342) {
  #      stmts;
  #    } else {
  #      stmts;
  #    }
  #  } finally {
  #    stmts;
  #    excHandler = excHandler.prev;
  #  }
  #
  var 
    i, length, blen: int
    safePoint, orExpr, epart: PRope
    a: TCompRes
  genLineDir(p, n, r)
  inc(p.unique)
  safePoint = ropef("Tmp$1", [toRope(p.unique)])
  appf(r.com, 
       "var $1 = {prev: excHandler, exc: null};$n" & "excHandler = $1;$n", 
       [safePoint])
  if optStackTrace in p.Options: app(r.com, "framePtr = F;" & tnl)
  app(r.com, "try {" & tnl)
  length = sonsLen(n)
  inc(p.nestedTryStmts)
  genStmt(p, n.sons[0], a)
  app(r.com, mergeStmt(a))
  i = 1
  epart = nil
  while (i < length) and (n.sons[i].kind == nkExceptBranch): 
    blen = sonsLen(n.sons[i])
    if blen == 1: 
      # general except section:
      if i > 1: app(epart, "else {" & tnl)
      genStmt(p, n.sons[i].sons[0], a)
      app(epart, mergeStmt(a))
      if i > 1: app(epart, '}' & tnl)
    else: 
      orExpr = nil
      for j in countup(0, blen - 2): 
        if (n.sons[i].sons[j].kind != nkType): 
          InternalError(n.info, "genTryStmt")
        if orExpr != nil: app(orExpr, "||")
        appf(orExpr, "($1.exc.m_type == $2)", 
             [safePoint, genTypeInfo(p, n.sons[i].sons[j].typ)])
      if i > 1: app(epart, "else ")
      appf(epart, "if ($1.exc && $2) {$n", [safePoint, orExpr])
      genStmt(p, n.sons[i].sons[blen - 1], a)
      appf(epart, "$1}$n", [mergeStmt(a)])
    inc(i)
  if epart != nil: appf(r.com, "} catch (EXC) {$n$1", [epart])
  finishTryStmt(p, r, p.nestedTryStmts)
  dec(p.nestedTryStmts)
  app(r.com, "} finally {" & tnl & "excHandler = excHandler.prev;" & tnl)
  if (i < length) and (n.sons[i].kind == nkFinally): 
    genStmt(p, n.sons[i].sons[0], a)
    app(r.com, mergeStmt(a))
  app(r.com, '}' & tnl)

proc genRaiseStmt(p: var TProc, n: PNode, r: var TCompRes) = 
  var 
    a: TCompRes
    typ: PType
  genLineDir(p, n, r)
  if n.sons[0].kind != nkEmpty: 
    gen(p, n.sons[0], a)
    if a.com != nil: appf(r.com, "$1;$n", [a.com])
    typ = skipTypes(n.sons[0].typ, abstractPtrs)
    useMagic(p, "raiseException")
    appf(r.com, "raiseException($1, $2);$n", 
         [a.res, makeCString(typ.sym.name.s)])
  else: 
    useMagic(p, "reraiseException")
    app(r.com, "reraiseException();" & tnl)

proc genCaseStmt(p: var TProc, n: PNode, r: var TCompRes) = 
  var 
    cond, stmt: TCompRes
    it, e, v: PNode
    stringSwitch: bool
  genLineDir(p, n, r)
  gen(p, n.sons[0], cond)
  if cond.com != nil: appf(r.com, "$1;$n", [cond.com])
  stringSwitch = skipTypes(n.sons[0].typ, abstractVar).kind == tyString
  if stringSwitch: 
    useMagic(p, "toEcmaStr")
    appf(r.com, "switch (toEcmaStr($1)) {$n", [cond.res])
  else: 
    appf(r.com, "switch ($1) {$n", [cond.res])
  for i in countup(1, sonsLen(n) - 1): 
    it = n.sons[i]
    case it.kind
    of nkOfBranch: 
      for j in countup(0, sonsLen(it) - 2): 
        e = it.sons[j]
        if e.kind == nkRange: 
          v = copyNode(e.sons[0])
          while (v.intVal <= e.sons[1].intVal): 
            gen(p, v, cond)
            if cond.com != nil: internalError(v.info, "ecmasgen.genCaseStmt")
            appf(r.com, "case $1: ", [cond.res])
            Inc(v.intVal)
        else: 
          gen(p, e, cond)
          if cond.com != nil: internalError(e.info, "ecmasgen.genCaseStmt")
          if stringSwitch: 
            case e.kind
            of nkStrLit..nkTripleStrLit: appf(r.com, "case $1: ", 
                [makeCString(e.strVal)])
            else: InternalError(e.info, "ecmasgen.genCaseStmt: 2")
          else: 
            appf(r.com, "case $1: ", [cond.res])
      genStmt(p, lastSon(it), stmt)
      appf(r.com, "$n$1break;$n", [mergeStmt(stmt)])
    of nkElse: 
      genStmt(p, it.sons[0], stmt)
      appf(r.com, "default: $n$1break;$n", [mergeStmt(stmt)])
    else: internalError(it.info, "ecmasgen.genCaseStmt")
  appf(r.com, "}$n", [])

proc genStmtListExpr(p: var TProc, n: PNode, r: var TCompRes)
proc genBlock(p: var TProc, n: PNode, r: var TCompRes) = 
  var 
    idx, labl: int
    sym: PSym
  inc(p.unique)
  idx = len(p.blocks)
  if n.sons[0].kind != nkEmpty: 
    # named block?
    if (n.sons[0].kind != nkSym): InternalError(n.info, "genBlock")
    sym = n.sons[0].sym
    sym.loc.k = locOther
    sym.loc.a = idx
  setlen(p.blocks, idx + 1)
  p.blocks[idx].id = - p.unique # negative because it isn't used yet
  p.blocks[idx].nestedTryStmts = p.nestedTryStmts
  labl = p.unique
  if n.kind == nkBlockExpr: genStmtListExpr(p, n.sons[1], r)
  else: genStmt(p, n.sons[1], r)
  if p.blocks[idx].id > 0: 
    # label has been used:
    r.com = ropef("L$1: do {$n$2} while(false);$n", [toRope(labl), r.com])
  setlen(p.blocks, idx)

proc genBreakStmt(p: var TProc, n: PNode, r: var TCompRes) = 
  var 
    idx: int
    sym: PSym
  genLineDir(p, n, r)
  idx = len(p.blocks) - 1
  if n.sons[0].kind != nkEmpty: 
    # named break?
    assert(n.sons[0].kind == nkSym)
    sym = n.sons[0].sym
    assert(sym.loc.k == locOther)
    idx = sym.loc.a
  p.blocks[idx].id = abs(p.blocks[idx].id) # label is used
  finishTryStmt(p, r, p.nestedTryStmts - p.blocks[idx].nestedTryStmts)
  appf(r.com, "break L$1;$n", [toRope(p.blocks[idx].id)])

proc genAsmStmt(p: var TProc, n: PNode, r: var TCompRes) = 
  genLineDir(p, n, r)
  assert(n.kind == nkAsmStmt)
  for i in countup(0, sonsLen(n) - 1): 
    case n.sons[i].Kind
    of nkStrLit..nkTripleStrLit: app(r.com, n.sons[i].strVal)
    of nkSym: app(r.com, mangleName(n.sons[i].sym))
    else: InternalError(n.sons[i].info, "ecmasgen: genAsmStmt()")
  
proc genIfStmt(p: var TProc, n: PNode, r: var TCompRes) = 
  var 
    toClose: int
    cond, stmt: TCompRes
    it: PNode
  toClose = 0
  for i in countup(0, sonsLen(n) - 1): 
    it = n.sons[i]
    if sonsLen(it) != 1: 
      gen(p, it.sons[0], cond)
      genStmt(p, it.sons[1], stmt)
      if i > 0: 
        appf(r.com, "else {$n", [])
        inc(toClose)
      if cond.com != nil: appf(r.com, "$1;$n", [cond.com])
      appf(r.com, "if ($1) {$n$2}", [cond.res, mergeStmt(stmt)])
    else: 
      # else part:
      genStmt(p, it.sons[0], stmt)
      appf(r.com, "else {$n$1}$n", [mergeStmt(stmt)])
  app(r.com, repeatChar(toClose, '}') & tnl)

proc genIfExpr(p: var TProc, n: PNode, r: var TCompRes) = 
  var 
    toClose: int
    cond, stmt: TCompRes
    it: PNode
  toClose = 0
  for i in countup(0, sonsLen(n) - 1): 
    it = n.sons[i]
    if sonsLen(it) != 1: 
      gen(p, it.sons[0], cond)
      gen(p, it.sons[1], stmt)
      if i > 0: 
        app(r.res, ": (")
        inc(toClose)
      r.com = mergeExpr(r.com, cond.com)
      r.com = mergeExpr(r.com, stmt.com)
      appf(r.res, "($1) ? ($2)", [cond.res, stmt.res])
    else: 
      # else part:
      gen(p, it.sons[0], stmt)
      r.com = mergeExpr(r.com, stmt.com)
      appf(r.res, ": ($1)", [stmt.res])
  app(r.res, repeatChar(toClose, ')'))

proc generateHeader(p: var TProc, typ: PType): PRope = 
  var 
    param: PSym
    name: PRope
  result = nil
  for i in countup(1, sonsLen(typ.n) - 1): 
    if result != nil: app(result, ", ")
    assert(typ.n.sons[i].kind == nkSym)
    param = typ.n.sons[i].sym
    name = mangleName(param)
    app(result, name)
    if mapType(param.typ) == etyBaseIndex: 
      app(result, ", ")
      app(result, name)
      app(result, "_Idx")

const 
  nodeKindsNeedNoCopy = {nkCharLit..nkInt64Lit, nkStrLit..nkTripleStrLit, 
    nkFloatLit..nkFloat64Lit, nkCurly, nkPar, nkStringToCString, 
    nkCStringToString, nkCall, nkPrefix, nkPostfix, nkInfix, 
    nkCommand, nkHiddenCallConv, nkCallStrLit}

proc needsNoCopy(y: PNode): bool = 
  result = (y.kind in nodeKindsNeedNoCopy) or
      (skipTypes(y.typ, abstractInst).kind in {tyRef, tyPtr, tyVar})

proc genAsgnAux(p: var TProc, x, y: PNode, r: var TCompRes, 
                noCopyNeeded: bool) = 
  var a, b: TCompRes
  gen(p, x, a)
  gen(p, y, b)
  case mapType(x.typ)
  of etyObject: 
    if a.com != nil: appf(r.com, "$1;$n", [a.com])
    if b.com != nil: appf(r.com, "$1;$n", [b.com])
    if needsNoCopy(y) or noCopyNeeded: 
      appf(r.com, "$1 = $2;$n", [a.res, b.res])
    else: 
      useMagic(p, "NimCopy")
      appf(r.com, "$1 = NimCopy($2, $3);$n", 
           [a.res, b.res, genTypeInfo(p, y.typ)])
  of etyBaseIndex: 
    if (a.kind != etyBaseIndex) or (b.kind != etyBaseIndex): 
      internalError(x.info, "genAsgn")
    appf(r.com, "$1 = $2; $3 = $4;$n", [a.com, b.com, a.res, b.res])
  else: 
    if a.com != nil: appf(r.com, "$1;$n", [a.com])
    if b.com != nil: appf(r.com, "$1;$n", [b.com])
    appf(r.com, "$1 = $2;$n", [a.res, b.res])

proc genAsgn(p: var TProc, n: PNode, r: var TCompRes) = 
  genLineDir(p, n, r)
  genAsgnAux(p, n.sons[0], n.sons[1], r, false)

proc genFastAsgn(p: var TProc, n: PNode, r: var TCompRes) = 
  genLineDir(p, n, r)
  genAsgnAux(p, n.sons[0], n.sons[1], r, true)

proc genSwap(p: var TProc, n: PNode, r: var TCompRes) = 
  var a, b: TCompRes
  gen(p, n.sons[1], a)
  gen(p, n.sons[2], b)
  inc(p.unique)
  var tmp = ropef("Tmp$1", [toRope(p.unique)])
  case mapType(n.sons[1].typ)
  of etyBaseIndex: 
    inc(p.unique)
    var tmp2 = ropef("Tmp$1", [toRope(p.unique)])
    if (a.kind != etyBaseIndex) or (b.kind != etyBaseIndex): 
      internalError(n.info, "genSwap")
    appf(r.com, "var $1 = $2; $2 = $3; $3 = $1;$n", [tmp, a.com, b.com])
    appf(r.com, "var $1 = $2; $2 = $3; $3 = $1", [tmp2, a.res, b.res])
  else: 
    if a.com != nil: appf(r.com, "$1;$n", [a.com])
    if b.com != nil: appf(r.com, "$1;$n", [b.com])
    appf(r.com, "var $1 = $2; $2 = $3; $3 = $1", [tmp, a.res, b.res])

proc genFieldAddr(p: var TProc, n: PNode, r: var TCompRes) = 
  var a: TCompRes
  r.kind = etyBaseIndex
  var b = if n.kind == nkHiddenAddr: n.sons[0] else: n
  gen(p, b.sons[0], a)
  if b.sons[1].kind != nkSym: InternalError(b.sons[1].info, "genFieldAddr")
  var f = b.sons[1].sym
  if f.loc.r == nil: f.loc.r = mangleName(f)
  r.res = makeCString(ropeToStr(f.loc.r))
  r.com = mergeExpr(a)

proc genFieldAccess(p: var TProc, n: PNode, r: var TCompRes) = 
  r.kind = etyNone
  gen(p, n.sons[0], r)
  if n.sons[1].kind != nkSym: InternalError(n.sons[1].info, "genFieldAddr")
  var f = n.sons[1].sym
  if f.loc.r == nil: f.loc.r = mangleName(f)
  r.res = ropef("$1.$2", [r.res, f.loc.r])

proc genCheckedFieldAddr(p: var TProc, n: PNode, r: var TCompRes) = 
  genFieldAddr(p, n.sons[0], r) # XXX
  
proc genCheckedFieldAccess(p: var TProc, n: PNode, r: var TCompRes) = 
  genFieldAccess(p, n.sons[0], r) # XXX
  
proc genArrayAddr(p: var TProc, n: PNode, r: var TCompRes) = 
  var 
    a, b: TCompRes
    first: biggestInt
  r.kind = etyBaseIndex
  gen(p, n.sons[0], a)
  gen(p, n.sons[1], b)
  r.com = mergeExpr(a)
  var typ = skipTypes(n.sons[0].typ, abstractPtrs)
  if typ.kind in {tyArray, tyArrayConstr}: first = FirstOrd(typ.sons[0])
  else: first = 0
  if (optBoundsCheck in p.options) and not isConstExpr(n.sons[1]): 
    useMagic(p, "chckIndx")
    b.res = ropef("chckIndx($1, $2, $3.length)-$2", 
                  [b.res, toRope(first), a.res]) 
    # XXX: BUG: a.res evaluated twice!
  elif first != 0: 
    b.res = ropef("($1)-$2", [b.res, toRope(first)])
  r.res = mergeExpr(b)

proc genArrayAccess(p: var TProc, n: PNode, r: var TCompRes) = 
  genArrayAddr(p, n, r)
  r.kind = etyNone
  r.res = ropef("$1[$2]", [r.com, r.res])
  r.com = nil

proc genAddr(p: var TProc, n: PNode, r: var TCompRes) = 
  var s: PSym
  case n.sons[0].kind
  of nkSym: 
    s = n.sons[0].sym
    if s.loc.r == nil: InternalError(n.info, "genAddr: 3")
    case s.kind
    of skVar, skLet, skResult: 
      if mapType(n.typ) == etyObject: 
        # make addr() a no-op:
        r.kind = etyNone
        r.res = s.loc.r
        r.com = nil
      elif sfGlobal in s.flags: 
        # globals are always indirect accessible
        r.kind = etyBaseIndex
        r.com = toRope("Globals")
        r.res = makeCString(ropeToStr(s.loc.r))
      elif sfAddrTaken in s.flags: 
        r.kind = etyBaseIndex
        r.com = s.loc.r
        r.res = toRope("0")
      else: 
        InternalError(n.info, "genAddr: 4")
    else: InternalError(n.info, "genAddr: 2")
  of nkCheckedFieldExpr: 
    genCheckedFieldAddr(p, n, r)
  of nkDotExpr: 
    genFieldAddr(p, n, r)
  of nkBracketExpr: 
    genArrayAddr(p, n, r)
  else: InternalError(n.info, "genAddr")
  
proc genSym(p: var TProc, n: PNode, r: var TCompRes) = 
  var s = n.sym
  if s.loc.r == nil: 
    InternalError(n.info, "symbol has no generated name: " & s.name.s)
  case s.kind
  of skVar, skLet, skParam, skTemp, skResult: 
    var k = mapType(s.typ)
    if k == etyBaseIndex: 
      r.kind = etyBaseIndex
      if {sfAddrTaken, sfGlobal} * s.flags != {}: 
        r.com = ropef("$1[0]", [s.loc.r])
        r.res = ropef("$1[1]", [s.loc.r])
      else: 
        r.com = s.loc.r
        r.res = con(s.loc.r, "_Idx")
    elif (k != etyObject) and (sfAddrTaken in s.flags): 
      r.res = ropef("$1[0]", [s.loc.r])
    else: 
      r.res = s.loc.r
  else: r.res = s.loc.r
  
proc genDeref(p: var TProc, n: PNode, r: var TCompRes) = 
  var a: TCompRes
  if mapType(n.sons[0].typ) == etyObject: 
    gen(p, n.sons[0], r)
  else: 
    gen(p, n.sons[0], a)
    if a.kind != etyBaseIndex: InternalError(n.info, "genDeref")
    r.res = ropef("$1[$2]", [a.com, a.res])

proc genArgs(p: var TProc, n: PNode, r: var TCompRes) =
  app(r.res, "(")
  for i in countup(1, sonsLen(n) - 1): 
    if i > 1: app(r.res, ", ")
    var a: TCompRes
    gen(p, n.sons[i], a)
    if a.kind == etyBaseIndex: 
      app(r.res, a.com)
      app(r.res, ", ")
      app(r.res, a.res)
    else: 
      app(r.res, mergeExpr(a))
  app(r.res, ")")

proc genCall(p: var TProc, n: PNode, r: var TCompRes) = 
  gen(p, n.sons[0], r)
  genArgs(p, n, r)

proc genEcho(p: var TProc, n: PNode, r: var TCompRes) =
  app(r.res, "rawEcho")
  genArgs(p, n, r)

proc putToSeq(s: string, indirect: bool): PRope = 
  result = toRope(s)
  if indirect: result = ropef("[$1]", [result])
  
proc createVar(p: var TProc, typ: PType, indirect: bool): PRope
proc createRecordVarAux(p: var TProc, rec: PNode, c: var int): PRope = 
  result = nil
  case rec.kind
  of nkRecList: 
    for i in countup(0, sonsLen(rec) - 1): 
      app(result, createRecordVarAux(p, rec.sons[i], c))
  of nkRecCase: 
    app(result, createRecordVarAux(p, rec.sons[0], c))
    for i in countup(1, sonsLen(rec) - 1): 
      app(result, createRecordVarAux(p, lastSon(rec.sons[i]), c))
  of nkSym: 
    if c > 0: app(result, ", ")
    app(result, mangleName(rec.sym))
    app(result, ": ")
    app(result, createVar(p, rec.sym.typ, false))
    inc(c)
  else: InternalError(rec.info, "createRecordVarAux")
  
proc createVar(p: var TProc, typ: PType, indirect: bool): PRope = 
  var t = skipTypes(typ, abstractInst)
  case t.kind
  of tyInt..tyInt64, tyEnum, tyChar: 
    result = putToSeq("0", indirect)
  of tyFloat..tyFloat128: 
    result = putToSeq("0.0", indirect)
  of tyRange, tyGenericInst: 
    result = createVar(p, lastSon(typ), indirect)
  of tySet: 
    result = toRope("{}")
  of tyBool: 
    result = putToSeq("false", indirect)
  of tyArray, tyArrayConstr: 
    var length = int(lengthOrd(t))
    var e = elemType(t)
    if length > 32: 
      useMagic(p, "ArrayConstr")
      result = ropef("ArrayConstr($1, $2, $3)", [toRope(length), 
          createVar(p, e, false), genTypeInfo(p, e)])
    else: 
      result = toRope("[")
      var i = 0
      while i < length: 
        if i > 0: app(result, ", ")
        app(result, createVar(p, e, false))
        inc(i)
      app(result, "]")
  of tyTuple: 
    result = toRope("{")
    var c = 0
    app(result, createRecordVarAux(p, t.n, c))
    app(result, "}")
  of tyObject: 
    result = toRope("{")
    var c = 0
    if not (tfFinal in t.flags) or (t.sons[0] != nil): 
      inc(c)
      appf(result, "m_type: $1", [genTypeInfo(p, t)])
    while t != nil: 
      app(result, createRecordVarAux(p, t.n, c))
      t = t.sons[0]
    app(result, "}")
  of tyVar, tyPtr, tyRef: 
    if mapType(t) == etyBaseIndex: result = putToSeq("[null, 0]", indirect)
    else: result = putToSeq("null", indirect)
  of tySequence, tyString, tyCString, tyPointer, tyProc: 
    result = putToSeq("null", indirect)
  else: 
    internalError("createVar: " & $t.kind)
    result = nil

proc isIndirect(v: PSym): bool = 
  result = (sfAddrTaken in v.flags) and (mapType(v.typ) != etyObject)

proc genVarInit(p: var TProc, v: PSym, n: PNode, r: var TCompRes) = 
  var 
    a: TCompRes
    s: PRope
  if n.kind == nkEmpty: 
    appf(r.com, "var $1 = $2;$n", 
         [mangleName(v), createVar(p, v.typ, isIndirect(v))])
  else: 
    discard mangleName(v)
    gen(p, n, a)
    case mapType(v.typ)
    of etyObject: 
      if a.com != nil: appf(r.com, "$1;$n", [a.com])
      if needsNoCopy(n): 
        s = a.res
      else: 
        useMagic(p, "NimCopy")
        s = ropef("NimCopy($1, $2)", [a.res, genTypeInfo(p, n.typ)])
    of etyBaseIndex: 
      if (a.kind != etyBaseIndex): InternalError(n.info, "genVarInit")
      if {sfAddrTaken, sfGlobal} * v.flags != {}: 
        appf(r.com, "var $1 = [$2, $3];$n", [v.loc.r, a.com, a.res])
      else: 
        appf(r.com, "var $1 = $2; var $1_Idx = $3;$n", [v.loc.r, a.com, a.res])
      return 
    else: 
      if a.com != nil: appf(r.com, "$1;$n", [a.com])
      s = a.res
    if isIndirect(v): appf(r.com, "var $1 = [$2];$n", [v.loc.r, s])
    else: appf(r.com, "var $1 = $2;$n", [v.loc.r, s])
  
proc genVarStmt(p: var TProc, n: PNode, r: var TCompRes) = 
  for i in countup(0, sonsLen(n) - 1): 
    var a = n.sons[i]
    if a.kind == nkCommentStmt: continue 
    assert(a.kind == nkIdentDefs)
    assert(a.sons[0].kind == nkSym)
    var v = a.sons[0].sym
    if lfNoDecl in v.loc.flags: continue 
    genLineDir(p, a, r)
    genVarInit(p, v, a.sons[2], r)

proc genConstStmt(p: var TProc, n: PNode, r: var TCompRes) =
  genLineDir(p, n, r)
  for i in countup(0, sonsLen(n) - 1):
    if n.sons[i].kind == nkCommentStmt: continue 
    assert(n.sons[i].kind == nkConstDef)
    var c = n.sons[i].sons[0].sym
    if (c.ast != nil) and (c.typ.kind in ConstantDataTypes) and
        not (lfNoDecl in c.loc.flags):
      genLineDir(p, n.sons[i], r)
      genVarInit(p, c, c.ast, r)

proc genNew(p: var TProc, n: PNode, r: var TCompRes) =
  var a: TCompRes
  gen(p, n.sons[1], a)
  var t = skipTypes(n.sons[1].typ, abstractVar).sons[0]
  if a.com != nil: appf(r.com, "$1;$n", [a.com])
  appf(r.com, "$1 = $2;$n", [a.res, createVar(p, t, true)])

proc genOrd(p: var TProc, n: PNode, r: var TCompRes) =
  case skipTypes(n.sons[1].typ, abstractVar).kind
  of tyEnum, tyInt..tyInt64, tyChar: gen(p, n.sons[1], r)
  of tyBool: unaryExpr(p, n, r, "", "($1 ? 1:0)")
  else: InternalError(n.info, "genOrd")
  
proc genConStrStr(p: var TProc, n: PNode, r: var TCompRes) =
  var a: TCompRes

  gen(p, n.sons[1], a)
  r.com = mergeExpr(r.com, a.com)
  if skipTypes(n.sons[1].typ, abstractVarRange).kind == tyChar:
    r.res.app(ropef("[$1].concat(", [a.res]))
  else:
    r.res.app(ropef("($1.slice(0,-1)).concat(", [a.res]))

  for i in countup(2, sonsLen(n) - 2):
    gen(p, n.sons[i], a)
    r.com = mergeExpr(r.com, a.com)

    if skipTypes(n.sons[i].typ, abstractVarRange).kind == tyChar:
      r.res.app(ropef("[$1],", [a.res]))
    else:
      r.res.app(ropef("$1.slice(0,-1),", [a.res]))

  gen(p, n.sons[sonsLen(n) - 1], a)
  r.com = mergeExpr(r.com, a.com)
  if skipTypes(n.sons[sonsLen(n) - 1].typ, abstractVarRange).kind == tyChar:
    r.res.app(ropef("[$1, 0])", [a.res]))
  else:
    r.res.app(ropef("$1)", [a.res]))

proc genRepr(p: var TProc, n: PNode, r: var TCompRes) =
  var t = skipTypes(n.sons[1].typ, abstractVarRange)
  case t.kind
  of tyInt..tyInt64:
    unaryExpr(p, n, r, "", "reprInt($1)")
  of tyEnum, tyOrdinal:
    binaryExpr(p, n, r, "", "reprEnum($1, $2)")
  else:
    # XXX:
    internalError(n.info, "genRepr: Not implemented")

proc genMagic(p: var TProc, n: PNode, r: var TCompRes) =
  var 
    a: TCompRes
    line, filen: PRope
  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, r)
  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 mPred:
    # XXX: range checking?
    if not (optOverflowCheck in p.Options): binaryExpr(p, n, r, "", "$1 - $2")
    else: binaryExpr(p, n, r, "subInt", "subInt($1, $2)")
  of mSucc:
    # XXX: range checking?
    if not (optOverflowCheck in p.Options): binaryExpr(p, n, r, "", "$1 - $2")
    else: binaryExpr(p, n, r, "addInt", "addInt($1, $2)")
  of mAppendStrCh: binaryStmt(p, n, r, "addChar", "$1 = addChar($1, $2)")
  of mAppendStrStr:
    if skipTypes(n.sons[1].typ, abstractVarRange).kind == tyCString:
        binaryStmt(p, n, r, "", "$1 += $2")
    else:
      binaryStmt(p, n, r, "", "$1 = ($1.slice(0,-1)).concat($2)")
    # XXX: make a copy of $2, because of ECMAScript's sucking semantics
  of mAppendSeqElem: binaryStmt(p, n, r, "", "$1.push($2)")
  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: unaryExpr(p, n, r, "", "$1 == null")
  of mEnumToStr: genRepr(p, n, r)
  of mNew, mNewFinalize: genNew(p, n, r)
  of mSizeOf: r.res = toRope(getSize(n.sons[1].typ))
  of mChr, mArrToSeq: gen(p, n.sons[1], r)      # nothing to do
  of mOrd: genOrd(p, n, r)
  of mLengthStr: unaryExpr(p, n, r, "", "($1.length-1)")
  of mLengthSeq, mLengthOpenArray, mLengthArray:
    unaryExpr(p, n, r, "", "$1.length")
  of mHigh:
    if skipTypes(n.sons[0].typ, abstractVar).kind == tyString:
      unaryExpr(p, n, r, "", "($1.length-2)")
    else:
      unaryExpr(p, n, r, "", "($1.length-1)")
  of mInc:
    if not (optOverflowCheck in p.Options): binaryStmt(p, n, r, "", "$1 += $2")
    else: binaryStmt(p, n, r, "addInt", "$1 = addInt($1, $2)")
  of ast.mDec:
    if not (optOverflowCheck in p.Options): binaryStmt(p, n, r, "", "$1 -= $2")
    else: binaryStmt(p, n, r, "subInt", "$1 = subInt($1, $2)")
  of mSetLengthStr: binaryStmt(p, n, r, "", "$1.length = ($2)-1")
  of mSetLengthSeq: binaryStmt(p, n, r, "", "$1.length = $2")
  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: binaryStmt(p, n, r, "", "$1[$2] = true")
  of mExcl: binaryStmt(p, n, r, "", "delete $1[$2]")
  of mInSet: binaryExpr(p, n, r, "", "($1[$2] != undefined)")
  of mNLen..mNError:
    localError(n.info, errCannotGenerateCodeForX, n.sons[0].sym.name.s)
  of mNewSeq: binaryStmt(p, n, r, "", "$1 = new Array($2)")
  of mEcho: genEcho(p, n, r)
  else:
    genCall(p, n, r)
    #else internalError(e.info, 'genMagic: ' + magicToStr[op]);
  
proc genSetConstr(p: var TProc, n: PNode, r: var TCompRes) = 
  var 
    a, b: TCompRes
  useMagic(p, "SetConstr")
  r.res = toRope("SetConstr(")
  for i in countup(0, sonsLen(n) - 1): 
    if i > 0: app(r.res, ", ")
    var it = n.sons[i]
    if it.kind == nkRange: 
      gen(p, it.sons[0], a)
      gen(p, it.sons[1], b)
      r.com = mergeExpr(r.com, mergeExpr(a.com, b.com))
      appf(r.res, "[$1, $2]", [a.res, b.res])
    else: 
      gen(p, it, a)
      r.com = mergeExpr(r.com, a.com)
      app(r.res, a.res)
  app(r.res, ")")

proc genArrayConstr(p: var TProc, n: PNode, r: var TCompRes) = 
  var a: TCompRes
  r.res = toRope("[")
  for i in countup(0, sonsLen(n) - 1): 
    if i > 0: app(r.res, ", ")
    gen(p, n.sons[i], a)
    r.com = mergeExpr(r.com, a.com)
    app(r.res, a.res)
  app(r.res, "]")

proc genRecordConstr(p: var TProc, n: PNode, r: var TCompRes) = 
  var a: TCompRes
  var i = 0
  var length = sonsLen(n)
  r.res = toRope("{")
  while i < length: 
    if i > 0: app(r.res, ", ")
    if (n.sons[i].kind != nkSym): 
      internalError(n.sons[i].info, "genRecordConstr")
    gen(p, n.sons[i + 1], a)
    r.com = mergeExpr(r.com, a.com)
    appf(r.res, "$1: $2", [mangleName(n.sons[i].sym), a.res])
    inc(i, 2)

proc genConv(p: var TProc, 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) and (src.kind == tyBool): 
    r.res = ropef("(($1)? 1:0)", [r.res])
  
proc upConv(p: var TProc, n: PNode, r: var TCompRes) = 
  gen(p, n.sons[0], r)        # XXX
  
proc genRangeChck(p: var TProc, 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)
    r.com = mergeExpr(r.com, mergeExpr(a.com, b.com))
    useMagic(p, "chckRange")
    r.res = ropef("chckRange($1, $2, $3)", [r.res, a.res, b.res])

proc convStrToCStr(p: var TProc, 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(n.info, "convStrToCStr")
    useMagic(p, "toEcmaStr")
    r.res = ropef("toEcmaStr($1)", [r.res])

proc convCStrToStr(p: var TProc, 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(n.info, "convCStrToStr")
    useMagic(p, "cstrToNimstr")
    r.res = ropef("cstrToNimstr($1)", [r.res])

proc genReturnStmt(p: var TProc, n: PNode, r: var TCompRes) = 
  var a: TCompRes
  if p.procDef == nil: InternalError(n.info, "genReturnStmt")
  p.BeforeRetNeeded = true
  if (n.sons[0].kind != nkEmpty): 
    genStmt(p, n.sons[0], a)
    if a.com != nil: appf(r.com, "$1;$n", mergeStmt(a))
  else: 
    genLineDir(p, n, r)
  finishTryStmt(p, r, p.nestedTryStmts)
  app(r.com, "break BeforeRet;" & tnl)

proc genProcBody(p: var TProc, prc: PSym, r: TCompRes): PRope = 
  if optStackTrace in prc.options: 
    result = ropef("var F={procname:$1,prev:framePtr,filename:$2,line:0};$n" &
        "framePtr = F;$n", [makeCString(prc.owner.name.s & '.' & prc.name.s), 
                            makeCString(toFilename(prc.info))])
  else: 
    result = nil
  if p.beforeRetNeeded: 
    appf(result, "BeforeRet: do {$n$1} while (false); $n", [mergeStmt(r)])
  else: 
    app(result, mergeStmt(r))
  if prc.typ.callConv == ccSysCall: 
    result = ropef("try {$n$1} catch (e) {$n" &
        " alert(\"Unhandled exception:\\n\" + e.message + \"\\n\"$n}", [result])
  if optStackTrace in prc.options: 
    app(result, "framePtr = framePtr.prev;" & tnl)

proc genProc(oldProc: var TProc, prc: PSym, r: var TCompRes) = 
  var 
    p: TProc
    resultSym: PSym
    name, returnStmt, resultAsgn, header: PRope
    a: TCompRes
  initProc(p, oldProc.globals, oldProc.module, prc.ast, prc.options)
  returnStmt = nil
  resultAsgn = nil
  name = mangleName(prc)
  header = generateHeader(p, prc.typ)
  if (prc.typ.sons[0] != nil) and not (sfPure in prc.flags): 
    resultSym = prc.ast.sons[resultPos].sym
    resultAsgn = ropef("var $1 = $2;$n", [mangleName(resultSym), 
        createVar(p, resultSym.typ, isIndirect(resultSym))])
    gen(p, prc.ast.sons[resultPos], a)
    if a.com != nil: appf(returnStmt, "$1;$n", [a.com])
    returnStmt = ropef("return $1;$n", [a.res])
  genStmt(p, prc.getBody, r)
  r.com = ropef("function $1($2) {$n$3$4$5}$n", 
                [name, header, resultAsgn, genProcBody(p, prc, r), returnStmt])
  r.res = nil  
  
proc genStmtListExpr(p: var TProc, n: PNode, r: var TCompRes) = 
  var a: TCompRes
  # watch out this trick: ``function () { stmtList; return expr; }()``
  r.res = toRope("function () {")
  for i in countup(0, sonsLen(n) - 2): 
    genStmt(p, n.sons[i], a)
    app(r.res, mergeStmt(a))
  gen(p, lastSon(n), a)
  if a.com != nil: appf(r.res, "$1;$n", [a.com])
  appf(r.res, "return $1; }()", [a.res])

proc genStmt(p: var TProc, n: PNode, r: var TCompRes) = 
  var a: TCompRes
  r.kind = etyNone
  r.com = nil
  r.res = nil
  case n.kind
  of nkNilLit, nkEmpty: nil
  of nkStmtList: 
    for i in countup(0, sonsLen(n) - 1): 
      genStmt(p, n.sons[i], a)
      app(r.com, mergeStmt(a))
  of nkBlockStmt: genBlock(p, n, r)
  of nkIfStmt: genIfStmt(p, n, r)
  of nkWhileStmt: genWhileStmt(p, n, r)
  of nkVarSection, nkLetSection: genVarStmt(p, n, r)
  of nkConstSection: genConstStmt(p, n, r)
  of nkForStmt: internalError(n.info, "for statement not eliminated")
  of nkCaseStmt: genCaseStmt(p, n, r)
  of nkReturnStmt: genReturnStmt(p, n, r)
  of nkBreakStmt: genBreakStmt(p, n, r)
  of nkAsgn: genAsgn(p, n, r)
  of nkFastAsgn: genFastAsgn(p, n, r)
  of nkDiscardStmt: 
    genLineDir(p, n, r)
    gen(p, n.sons[0], r)
    app(r.res, ';' & tnl)
  of nkAsmStmt: genAsmStmt(p, n, r)
  of nkTryStmt: genTryStmt(p, n, r)
  of nkRaiseStmt: genRaiseStmt(p, n, r)
  of nkTypeSection, nkCommentStmt, nkIteratorDef, nkIncludeStmt, nkImportStmt, 
     nkFromStmt, nkTemplateDef, nkMacroDef, nkPragma: 
    nil
  of nkProcDef, nkMethodDef, nkConverterDef: 
    if (n.sons[genericParamsPos].kind == nkEmpty): 
      var prc = n.sons[namePos].sym
      if lfNoDecl notin prc.loc.flags and prc.getBody.kind != nkEmpty:
        genProc(p, prc, r)
      else:
        discard mangleName(prc)
  else: 
    genLineDir(p, n, r)
    if n.sons[0].kind == nkSym:
      if n.sons[0].sym.loc.r == nil:
        n.sons[0].sym.loc.r = toRope(n.sons[0].sym.name.s)
    gen(p, n, r)
    app(r.res, ';' & tnl)

proc gen(p: var TProc, n: PNode, r: var TCompRes) = 
  var f: BiggestFloat
  r.kind = etyNone
  r.com = nil
  r.res = nil
  case n.kind
  of nkSym: 
    genSym(p, n, r)
  of nkCharLit..nkInt64Lit: 
    r.res = toRope(n.intVal)
  of nkNilLit: 
    if mapType(n.typ) == etyBaseIndex: 
      r.kind = etyBaseIndex
      r.com = toRope"null"
      r.res = toRope"0"
    else: 
      r.res = toRope"null"
  of nkStrLit..nkTripleStrLit: 
    if skipTypes(n.typ, abstractVarRange).kind == tyString: 
      useMagic(p, "cstrToNimstr")
      r.res = ropef("cstrToNimstr($1)", [makeCString(n.strVal)])
    else: 
      r.res = makeCString(n.strVal)
  of nkFloatLit..nkFloat64Lit: 
    f = n.floatVal
    if f != f: r.res = toRope"NaN"
    elif f == 0.0: r.res = toRope"0.0"
    elif f == 0.5 * f: 
      if f > 0.0: r.res = toRope"Infinity"
      else: r.res = toRope"-Infinity"
    else: r.res = toRope(f.ToStrMaxPrecision)
  of nkBlockExpr: genBlock(p, n, r)
  of nkIfExpr: genIfExpr(p, n, r)
  of nkCallKinds: 
    if (n.sons[0].kind == nkSym) and (n.sons[0].sym.magic != mNone): 
      genMagic(p, n, r)
    else: 
      genCall(p, n, r)
  of nkCurly: genSetConstr(p, n, r)
  of nkBracket: genArrayConstr(p, n, r)
  of nkPar: genRecordConstr(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: genCheckedFieldAccess(p, n, r)
  of nkObjDownConv: gen(p, n.sons[0], r)
  of nkObjUpConv: upConv(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 nkStmtListExpr: genStmtListExpr(p, n, r)
  of nkEmpty: nil
  of nkMetaNode: gen(p, n.sons[0], r)
  of nkType: r.res = genTypeInfo(p, n.typ)
  else: InternalError(n.info, "gen: unknown node type: " & $n.kind)
  
var globals: PGlobals

proc newModule(module: PSym, filename: string): BModule = 
  new(result)
  result.filename = filename
  result.module = module
  if globals == nil: globals = newGlobals()
  
proc genHeader(): PRope = 
  result = ropef("/* Generated by the Nimrod Compiler v$1 */$n" &
      "/*   (c) 2012 Andreas Rumpf */$n$n" & "$nvar Globals = this;$n" &
      "var framePtr = null;$n" & "var excHandler = null;$n", 
                 [toRope(versionAsString)])

proc genModule(p: var TProc, n: PNode, r: var TCompRes) = 
  genStmt(p, n, r)
  if optStackTrace in p.options: 
    r.com = ropef("var F = {procname:$1,prev:framePtr,filename:$2,line:0};$n" &
        "framePtr = F;$n" & "$3" & "framePtr = framePtr.prev;$n", [
        makeCString("module " & p.module.module.name.s), 
        makeCString(toFilename(p.module.module.info)), r.com])

proc myProcess(b: PPassContext, n: PNode): PNode = 
  if passes.skipCodegen(n): return n
  var 
    p: TProc
    r: TCompRes
  result = n
  var m = BModule(b)
  if m.module == nil: InternalError(n.info, "myProcess")
  initProc(p, globals, m, nil, m.module.options)
  genModule(p, n, r)
  app(p.globals.code, p.data)
  app(p.globals.code, mergeStmt(r))

proc myClose(b: PPassContext, n: PNode): PNode = 
  if passes.skipCodegen(n): return n
  result = myProcess(b, n)
  var m = BModule(b)
  if sfMainModule in m.module.flags: 
    var disp = generateMethodDispatchers()
    for i in 0..sonsLen(disp)-1: 
      var 
        p: TProc
        r: TCompRes
      initProc(p, globals, m, nil, m.module.options)
      genProc(p, disp.sons[i].sym, r)
      app(p.globals.code, mergeStmt(r))

    # write the file:
    var code = con(globals.typeInfo, globals.code)
    var outfile = changeFileExt(completeCFilePath(m.filename), "js")
    discard writeRopeIfNotEqual(con(genHeader(), code), outfile)

proc myOpenCached(s: PSym, filename: string, rd: PRodReader): PPassContext = 
  InternalError("symbol files are not possible with the Ecmas code generator")
  result = nil

proc myOpen(s: PSym, filename: string): PPassContext = 
  result = newModule(s, filename)

proc ecmasgenPass(): TPass = 
  InitPass(result)
  result.open = myOpen
  result.close = myClose
  result.openCached = myOpenCached
  result.process = myProcess