Remove PHP backend. (#7606)

* remove php

* fix

* keep nimphpext

* remove targetJS enum
This commit is contained in:
treeform
2018-04-18 11:40:11 -07:00
committed by Andreas Rumpf
parent a708fda733
commit 35b0cc67e8
9 changed files with 330 additions and 778 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -121,26 +121,7 @@ proc genEnumInfo(p: PProc, typ: PType, name: Rope) =
addf(p.g.typeInfo, "$1.base = $2;$n",
[name, genTypeInfo(p, typ.sons[0])])
proc genEnumInfoPHP(p: PProc; t: PType): Rope =
let t = t.skipTypes({tyGenericInst, tyDistinct, tyAlias, tySink})
result = "$$NTI$1" % [rope(t.id)]
p.declareGlobal(t.id, result)
if containsOrIncl(p.g.typeInfoGenerated, t.id): return
let length = sonsLen(t.n)
var s: Rope = nil
for i in countup(0, length - 1):
if (t.n.sons[i].kind != nkSym): internalError(t.n.info, "genEnumInfo")
let field = t.n.sons[i].sym
if i > 0: add(s, ", " & tnl)
let extName = if field.ast == nil: field.name.s else: field.ast.strVal
addf(s, "$# => $#$n",
[rope(field.position), makeJSString(extName)])
prepend(p.g.typeInfo, "$$$# = $#;$n" % [result, s])
proc genTypeInfo(p: PProc, typ: PType): Rope =
if p.target == targetPHP:
return makeJSString(typeToString(typ, preferModuleInfo))
let t = typ.skipTypes({tyGenericInst, tyDistinct, tyAlias, tySink})
result = "NTI$1" % [rope(t.id)]
if containsOrIncl(p.g.typeInfoGenerated, t.id): return

View File

@@ -230,7 +230,7 @@ template isIterator*(owner: PSym): bool =
proc liftingHarmful(owner: PSym): bool {.inline.} =
## lambda lifting can be harmful for JS-like code generators.
let isCompileTime = sfCompileTime in owner.flags or owner.kind == skMacro
result = gCmd in {cmdCompileToPHP, cmdCompileToJS} and not isCompileTime
result = gCmd == cmdCompileToJS and not isCompileTime
proc liftIterSym*(n: PNode; owner: PSym): PNode =
# transforms (iter) to (let env = newClosure[iter](); (iter, env))
@@ -813,7 +813,7 @@ proc liftLambdas*(fn: PSym, body: PNode; tooEarly: var bool): PNode =
let isCompileTime = sfCompileTime in fn.flags or fn.kind == skMacro
if body.kind == nkEmpty or (
gCmd in {cmdCompileToPHP, cmdCompileToJS} and not isCompileTime) or
gCmd == cmdCompileToJS and not isCompileTime) or
fn.skipGenericOwner.kind != skModule:
# ignore forward declaration:
result = body

View File

@@ -94,7 +94,6 @@ proc commandCompileToJS(graph: ModuleGraph; cache: IdentCache) =
defineSymbol("nimrod") # 'nimrod' is always defined
defineSymbol("ecmascript") # For backward compatibility
defineSymbol("js")
if gCmd == cmdCompileToPHP: defineSymbol("nimphp")
semanticPasses()
registerPass(JSgenPass)
compileProject(graph, cache)
@@ -189,9 +188,6 @@ proc mainCommand*(graph: ModuleGraph; cache: IdentCache) =
of "js", "compiletojs":
gCmd = cmdCompileToJS
commandCompileToJS(graph, cache)
of "php":
gCmd = cmdCompileToPHP
commandCompileToJS(graph, cache)
of "doc0":
wantMainModule()
gCmd = cmdDoc

View File

@@ -90,14 +90,6 @@ proc handleCmdLine(cache: IdentCache; config: ConfigRef) =
ex = quoteShell(
completeCFilePath(changeFileExt(gProjectFull, "js").prependCurDir))
execExternalProgram(findNodeJs() & " " & ex & ' ' & commands.arguments)
elif gCmd == cmdCompileToPHP:
var ex: string
if options.outFile.len > 0:
ex = options.outFile.prependCurDir.quoteShell
else:
ex = quoteShell(
completeCFilePath(changeFileExt(gProjectFull, "php").prependCurDir))
execExternalProgram("php " & ex & ' ' & commands.arguments)
else:
var binPath: string
if options.outFile.len > 0:

View File

@@ -81,7 +81,6 @@ type
# **keep binary compatible**
cmdNone, cmdCompileToC, cmdCompileToCpp, cmdCompileToOC,
cmdCompileToJS,
cmdCompileToPHP,
cmdCompileToLLVM, cmdInterpret, cmdPretty, cmdDoc,
cmdGenDepend, cmdDump,
cmdCheck, # semantic checking for whole project

View File

@@ -586,7 +586,7 @@ proc cmdChangeTriggersRecompilation(old, new: TCommands): bool =
# new command forces us to consider it here :-)
case old
of cmdCompileToC, cmdCompileToCpp, cmdCompileToOC,
cmdCompileToJS, cmdCompileToPHP, cmdCompileToLLVM:
cmdCompileToJS, cmdCompileToLLVM:
if new in {cmdDoc, cmdCheck, cmdIdeTools, cmdPretty, cmdDef,
cmdInteractive}:
return false

View File

@@ -2404,7 +2404,7 @@ proc `==` *[T](x, y: seq[T]): bool {.noSideEffect.} =
if x.isNil and y.isNil:
return true
else:
when not defined(JS) or defined(nimphp):
when not defined(JS):
proc seqToPtr[T](x: seq[T]): pointer {.inline, nosideeffect.} =
result = cast[pointer](x)
else:
@@ -2766,17 +2766,14 @@ type
when defined(JS):
proc add*(x: var string, y: cstring) {.asmNoStackFrame.} =
when defined(nimphp):
asm """`x` .= `y`;"""
else:
asm """
var len = `x`[0].length-1;
for (var i = 0; i < `y`.length; ++i) {
`x`[0][len] = `y`.charCodeAt(i);
++len;
}
`x`[0][len] = 0
"""
asm """
var len = `x`[0].length-1;
for (var i = 0; i < `y`.length; ++i) {
`x`[0][len] = `y`.charCodeAt(i);
++len;
}
`x`[0][len] = 0
"""
proc add*(x: var cstring, y: cstring) {.magic: "AppendStrStr".}
elif hasAlloc:

View File

@@ -48,10 +48,7 @@ proc nimCharToStr(x: char): string {.compilerproc.} =
result[0] = x
proc isNimException(): bool {.asmNoStackFrame.} =
when defined(nimphp):
asm "return isset(`lastJSError`['m_type']);"
else:
asm "return `lastJSError`.m_type;"
asm "return `lastJSError`.m_type;"
proc getCurrentException*(): ref Exception {.compilerRtl, benign.} =
if isNimException(): result = cast[ref Exception](lastJSError)
@@ -61,15 +58,14 @@ proc getCurrentExceptionMsg*(): string =
if isNimException():
return cast[Exception](lastJSError).msg
else:
when not defined(nimphp):
var msg: cstring
{.emit: """
if (`lastJSError`.message !== undefined) {
`msg` = `lastJSError`.message;
}
""".}
if not msg.isNil:
return $msg
var msg: cstring
{.emit: """
if (`lastJSError`.message !== undefined) {
`msg` = `lastJSError`.message;
}
""".}
if not msg.isNil:
return $msg
return ""
proc auxWriteStackTrace(f: PCallFrame): string =
@@ -140,12 +136,9 @@ proc raiseException(e: ref Exception, ename: cstring) {.
e.name = ename
if excHandler == 0:
unhandledException(e)
when defined(nimphp):
asm """throw new Exception($`e`["message"]);"""
else:
when NimStackTrace:
e.trace = rawWriteStackTrace()
asm "throw `e`;"
when NimStackTrace:
e.trace = rawWriteStackTrace()
asm "throw `e`;"
proc reraiseException() {.compilerproc, asmNoStackFrame.} =
if lastJSError == nil:
@@ -173,57 +166,35 @@ proc raiseFieldError(f: string) {.compilerproc, noreturn.} =
raise newException(FieldError, f & " is not accessible")
proc setConstr() {.varargs, asmNoStackFrame, compilerproc.} =
when defined(nimphp):
asm """
$args = func_get_args();
$result = array();
foreach ($args as $x) {
if (is_array($x)) {
for ($j = $x[0]; $j <= $x[1]; $j++) {
$result[$j] = true;
}
} else {
$result[$x] = true;
asm """
var result = {};
for (var i = 0; i < arguments.length; ++i) {
var x = arguments[i];
if (typeof(x) == "object") {
for (var j = x[0]; j <= x[1]; ++j) {
result[j] = true;
}
} else {
result[x] = true;
}
return $result;
"""
else:
asm """
var result = {};
for (var i = 0; i < arguments.length; ++i) {
var x = arguments[i];
if (typeof(x) == "object") {
for (var j = x[0]; j <= x[1]; ++j) {
result[j] = true;
}
} else {
result[x] = true;
}
}
return result;
"""
}
return result;
"""
proc makeNimstrLit(c: cstring): string {.asmNoStackFrame, compilerproc.} =
when defined(nimphp):
{.emit: """return `c`;""".}
else:
{.emit: """
var ln = `c`.length;
var result = new Array(ln + 1);
var i = 0;
for (; i < ln; ++i) {
result[i] = `c`.charCodeAt(i);
}
result[i] = 0; // terminating zero
return result;
""".}
{.emit: """
var ln = `c`.length;
var result = new Array(ln + 1);
var i = 0;
for (; i < ln; ++i) {
result[i] = `c`.charCodeAt(i);
}
result[i] = 0; // terminating zero
return result;
""".}
proc cstrToNimstr(c: cstring): string {.asmNoStackFrame, compilerproc.} =
when defined(nimphp):
{.emit: """return `c`;""".}
else:
{.emit: """
{.emit: """
var ln = `c`.length;
var result = new Array(ln);
var r = 0;
@@ -261,156 +232,93 @@ proc cstrToNimstr(c: cstring): string {.asmNoStackFrame, compilerproc.} =
""".}
proc toJSStr(s: string): cstring {.asmNoStackFrame, compilerproc.} =
when defined(nimphp):
{.emit: """return `s`;""".}
else:
asm """
var len = `s`.length-1;
var asciiPart = new Array(len);
var fcc = String.fromCharCode;
var nonAsciiPart = null;
var nonAsciiOffset = 0;
for (var i = 0; i < len; ++i) {
if (nonAsciiPart !== null) {
var offset = (i - nonAsciiOffset) * 2;
var code = `s`[i].toString(16);
if (code.length == 1) {
code = "0"+code;
}
nonAsciiPart[offset] = "%";
nonAsciiPart[offset + 1] = code;
}
else if (`s`[i] < 128)
asciiPart[i] = fcc(`s`[i]);
else {
asciiPart.length = i;
nonAsciiOffset = i;
nonAsciiPart = new Array((len - i) * 2);
--i;
asm """
var len = `s`.length-1;
var asciiPart = new Array(len);
var fcc = String.fromCharCode;
var nonAsciiPart = null;
var nonAsciiOffset = 0;
for (var i = 0; i < len; ++i) {
if (nonAsciiPart !== null) {
var offset = (i - nonAsciiOffset) * 2;
var code = `s`[i].toString(16);
if (code.length == 1) {
code = "0"+code;
}
nonAsciiPart[offset] = "%";
nonAsciiPart[offset + 1] = code;
}
asciiPart = asciiPart.join("");
return (nonAsciiPart === null) ?
asciiPart : asciiPart + decodeURIComponent(nonAsciiPart.join(""));
else if (`s`[i] < 128)
asciiPart[i] = fcc(`s`[i]);
else {
asciiPart.length = i;
nonAsciiOffset = i;
nonAsciiPart = new Array((len - i) * 2);
--i;
}
}
asciiPart = asciiPart.join("");
return (nonAsciiPart === null) ?
asciiPart : asciiPart + decodeURIComponent(nonAsciiPart.join(""));
"""
proc mnewString(len: int): string {.asmNoStackFrame, compilerproc.} =
when defined(nimphp):
asm """
return str_repeat(chr(0),`len`);
"""
else:
asm """
var result = new Array(`len`+1);
result[0] = 0;
result[`len`] = 0;
return result;
"""
when defined(nimphp):
proc nimAt(x: string; i: int): string {.asmNoStackFrame, compilerproc.} =
asm """
return `x`[`i`];
"""
when defined(nimphp):
proc nimSubstr(s: string; a, b: int): string {.
asmNoStackFrame, compilerproc.} =
asm """return substr(`s`,`a`,`b`-`a`+1);"""
asm """
var result = new Array(`len`+1);
result[0] = 0;
result[`len`] = 0;
return result;
"""
proc SetCard(a: int): int {.compilerproc, asmNoStackFrame.} =
# argument type is a fake
when defined(nimphp):
asm """
return count(`a`);
"""
else:
asm """
var result = 0;
for (var elem in `a`) { ++result; }
return result;
"""
asm """
var result = 0;
for (var elem in `a`) { ++result; }
return result;
"""
proc SetEq(a, b: int): bool {.compilerproc, asmNoStackFrame.} =
when defined(nimphp):
asm """
foreach (`a` as $elem=>$_) { if (!isset(`b`[$elem])) return false; }
foreach (`b` as $elem=>$_) { if (!isset(`a`[$elem])) return false; }
return true;
"""
else:
asm """
for (var elem in `a`) { if (!`b`[elem]) return false; }
for (var elem in `b`) { if (!`a`[elem]) return false; }
return true;
"""
asm """
for (var elem in `a`) { if (!`b`[elem]) return false; }
for (var elem in `b`) { if (!`a`[elem]) return false; }
return true;
"""
proc SetLe(a, b: int): bool {.compilerproc, asmNoStackFrame.} =
when defined(nimphp):
asm """
foreach (`a` as $elem=>$_) { if (!isset(`b`[$elem])) return false; }
return true;
"""
else:
asm """
for (var elem in `a`) { if (!`b`[elem]) return false; }
return true;
"""
asm """
for (var elem in `a`) { if (!`b`[elem]) return false; }
return true;
"""
proc SetLt(a, b: int): bool {.compilerproc.} =
result = SetLe(a, b) and not SetEq(a, b)
proc SetMul(a, b: int): int {.compilerproc, asmNoStackFrame.} =
when defined(nimphp):
asm """
var $result = array();
foreach (`a` as $elem=>$_) {
if (isset(`b`[$elem])) { $result[$elem] = true; }
}
return $result;
"""
else:
asm """
var result = {};
for (var elem in `a`) {
if (`b`[elem]) { result[elem] = true; }
}
return result;
"""
asm """
var result = {};
for (var elem in `a`) {
if (`b`[elem]) { result[elem] = true; }
}
return result;
"""
proc SetPlus(a, b: int): int {.compilerproc, asmNoStackFrame.} =
when defined(nimphp):
asm """
var $result = array();
foreach (`a` as $elem=>$_) { $result[$elem] = true; }
foreach (`b` as $elem=>$_) { $result[$elem] = true; }
return $result;
"""
else:
asm """
var result = {};
for (var elem in `a`) { result[elem] = true; }
for (var elem in `b`) { result[elem] = true; }
return result;
"""
asm """
var result = {};
for (var elem in `a`) { result[elem] = true; }
for (var elem in `b`) { result[elem] = true; }
return result;
"""
proc SetMinus(a, b: int): int {.compilerproc, asmNoStackFrame.} =
when defined(nimphp):
asm """
$result = array();
foreach (`a` as $elem=>$_) {
if (!isset(`b`[$elem])) { $result[$elem] = true; }
}
return $result;
"""
else:
asm """
var result = {};
for (var elem in `a`) {
if (!`b`[elem]) { result[elem] = true; }
}
return result;
"""
asm """
var result = {};
for (var elem in `a`) {
if (!`b`[elem]) { result[elem] = true; }
}
return result;
"""
proc cmpStrings(a, b: string): int {.asmNoStackFrame, compilerProc.} =
asm """
@@ -424,15 +332,8 @@ proc cmpStrings(a, b: string): int {.asmNoStackFrame, compilerProc.} =
return `a`.length - `b`.length;
"""
proc cmp(x, y: string): int =
when defined(nimphp):
asm """
if(`x` < `y`) `result` = -1;
elseif (`x` > `y`) `result` = 1;
else `result` = 0;
"""
else:
return cmpStrings(x, y)
proc cmp(x, y: string): int =
return cmpStrings(x, y)
proc eqStrings(a, b: string): bool {.asmNoStackFrame, compilerProc.} =
asm """
@@ -467,7 +368,7 @@ elif not defined(nimOldEcho):
console.log(buf);
"""
elif not defined(nimphp):
else:
proc ewriteln(x: cstring) =
var node : JSRef
{.emit: "`node` = document.getElementsByTagName('body')[0];".}
@@ -493,127 +394,77 @@ elif not defined(nimphp):
# Arithmetic:
proc addInt(a, b: int): int {.asmNoStackFrame, compilerproc.} =
when defined(nimphp):
asm """
return `a` + `b`;
"""
else:
asm """
var result = `a` + `b`;
if (result > 2147483647 || result < -2147483648) `raiseOverflow`();
return result;
"""
asm """
var result = `a` + `b`;
if (result > 2147483647 || result < -2147483648) `raiseOverflow`();
return result;
"""
proc subInt(a, b: int): int {.asmNoStackFrame, compilerproc.} =
when defined(nimphp):
asm """
return `a` - `b`;
"""
else:
asm """
var result = `a` - `b`;
if (result > 2147483647 || result < -2147483648) `raiseOverflow`();
return result;
"""
asm """
var result = `a` - `b`;
if (result > 2147483647 || result < -2147483648) `raiseOverflow`();
return result;
"""
proc mulInt(a, b: int): int {.asmNoStackFrame, compilerproc.} =
when defined(nimphp):
asm """
return `a` * `b`;
"""
else:
asm """
var result = `a` * `b`;
if (result > 2147483647 || result < -2147483648) `raiseOverflow`();
return result;
"""
asm """
var result = `a` * `b`;
if (result > 2147483647 || result < -2147483648) `raiseOverflow`();
return result;
"""
proc divInt(a, b: int): int {.asmNoStackFrame, compilerproc.} =
when defined(nimphp):
asm """
return trunc(`a` / `b`);
"""
else:
asm """
if (`b` == 0) `raiseDivByZero`();
if (`b` == -1 && `a` == 2147483647) `raiseOverflow`();
return Math.trunc(`a` / `b`);
"""
asm """
if (`b` == 0) `raiseDivByZero`();
if (`b` == -1 && `a` == 2147483647) `raiseOverflow`();
return Math.trunc(`a` / `b`);
"""
proc modInt(a, b: int): int {.asmNoStackFrame, compilerproc.} =
when defined(nimphp):
asm """
return `a` % `b`;
"""
else:
asm """
if (`b` == 0) `raiseDivByZero`();
if (`b` == -1 && `a` == 2147483647) `raiseOverflow`();
return Math.trunc(`a` % `b`);
"""
asm """
if (`b` == 0) `raiseDivByZero`();
if (`b` == -1 && `a` == 2147483647) `raiseOverflow`();
return Math.trunc(`a` % `b`);
"""
proc addInt64(a, b: int): int {.asmNoStackFrame, compilerproc.} =
when defined(nimphp):
asm """
return `a` + `b`;
"""
else:
asm """
var result = `a` + `b`;
if (result > 9223372036854775807
|| result < -9223372036854775808) `raiseOverflow`();
return result;
"""
asm """
var result = `a` + `b`;
if (result > 9223372036854775807
|| result < -9223372036854775808) `raiseOverflow`();
return result;
"""
proc subInt64(a, b: int): int {.asmNoStackFrame, compilerproc.} =
when defined(nimphp):
asm """
return `a` - `b`;
"""
else:
asm """
var result = `a` - `b`;
if (result > 9223372036854775807
|| result < -9223372036854775808) `raiseOverflow`();
return result;
"""
asm """
var result = `a` - `b`;
if (result > 9223372036854775807
|| result < -9223372036854775808) `raiseOverflow`();
return result;
"""
proc mulInt64(a, b: int): int {.asmNoStackFrame, compilerproc.} =
when defined(nimphp):
asm """
return `a` * `b`;
"""
else:
asm """
var result = `a` * `b`;
if (result > 9223372036854775807
|| result < -9223372036854775808) `raiseOverflow`();
return result;
"""
asm """
var result = `a` * `b`;
if (result > 9223372036854775807
|| result < -9223372036854775808) `raiseOverflow`();
return result;
"""
proc divInt64(a, b: int): int {.asmNoStackFrame, compilerproc.} =
when defined(nimphp):
asm """
return trunc(`a` / `b`);
"""
else:
asm """
if (`b` == 0) `raiseDivByZero`();
if (`b` == -1 && `a` == 9223372036854775807) `raiseOverflow`();
return Math.trunc(`a` / `b`);
"""
asm """
if (`b` == 0) `raiseDivByZero`();
if (`b` == -1 && `a` == 9223372036854775807) `raiseOverflow`();
return Math.trunc(`a` / `b`);
"""
proc modInt64(a, b: int): int {.asmNoStackFrame, compilerproc.} =
when defined(nimphp):
asm """
return `a` % `b`;
"""
else:
asm """
if (`b` == 0) `raiseDivByZero`();
if (`b` == -1 && `a` == 9223372036854775807) `raiseOverflow`();
return Math.trunc(`a` % `b`);
"""
asm """
if (`b` == 0) `raiseDivByZero`();
if (`b` == -1 && `a` == 9223372036854775807) `raiseOverflow`();
return Math.trunc(`a` % `b`);
"""
proc negInt(a: int): int {.compilerproc.} =
result = a*(-1)
@@ -767,24 +618,14 @@ proc genericReset(x: JSRef, ti: PNimType): JSRef {.compilerproc.} =
else:
discard
when defined(nimphp):
proc arrayConstr(len: int, value: string, typ: string): JSRef {.
asmNoStackFrame, compilerproc.} =
# types are fake
asm """
$result = array();
for ($i = 0; $i < `len`; $i++) $result[] = `value`;
return $result;
"""
else:
proc arrayConstr(len: int, value: JSRef, typ: PNimType): JSRef {.
asmNoStackFrame, compilerproc.} =
proc arrayConstr(len: int, value: JSRef, typ: PNimType): JSRef {.
asmNoStackFrame, compilerproc.} =
# types are fake
asm """
var result = new Array(`len`);
for (var i = 0; i < `len`; ++i) result[i] = nimCopy(null, `value`, `typ`);
return result;
"""
asm """
var result = new Array(`len`);
for (var i = 0; i < `len`; ++i) result[i] = nimCopy(null, `value`, `typ`);
return result;
"""
proc chckIndx(i, a, b: int): int {.compilerproc.} =
if i >= a and i <= b: return i