first version of an PHP codegen

This commit is contained in:
Andreas Rumpf 2016-02-04 03:40:39 +01:00
commit 35567a1eb9
8 changed files with 665 additions and 411 deletions

File diff suppressed because it is too large Load diff

View file

@ -116,6 +116,8 @@ proc genEnumInfo(p: PProc, typ: PType, name: Rope) =
[name, genTypeInfo(p, typ.sons[0])]) [name, genTypeInfo(p, typ.sons[0])])
proc genTypeInfo(p: PProc, typ: PType): Rope = proc genTypeInfo(p: PProc, typ: PType): Rope =
if p.target == targetPHP:
return makeJSString(typeToString(typ, preferModuleInfo))
let t = typ.skipTypes({tyGenericInst, tyDistinct}) let t = typ.skipTypes({tyGenericInst, tyDistinct})
result = "NTI$1" % [rope(t.id)] result = "NTI$1" % [rope(t.id)]
if containsOrIncl(p.g.typeInfoGenerated, t.id): return if containsOrIncl(p.g.typeInfoGenerated, t.id): return

View file

@ -108,6 +108,7 @@ proc commandCompileToJS =
defineSymbol("nimrod") # 'nimrod' is always defined defineSymbol("nimrod") # 'nimrod' is always defined
defineSymbol("ecmascript") # For backward compatibility defineSymbol("ecmascript") # For backward compatibility
defineSymbol("js") defineSymbol("js")
if gCmd == cmdCompileToPHP: defineSymbol("nimphp")
semanticPasses() semanticPasses()
registerPass(JSgenPass) registerPass(JSgenPass)
compileProject() compileProject()
@ -267,6 +268,9 @@ proc mainCommand* =
of "js", "compiletojs": of "js", "compiletojs":
gCmd = cmdCompileToJS gCmd = cmdCompileToJS
commandCompileToJS() commandCompileToJS()
of "php":
gCmd = cmdCompileToPHP
commandCompileToJS()
of "doc": of "doc":
wantMainModule() wantMainModule()
gCmd = cmdDoc gCmd = cmdDoc

View file

@ -84,6 +84,14 @@ proc handleCmdLine() =
ex = quoteShell( ex = quoteShell(
completeCFilePath(changeFileExt(gProjectFull, "js").prependCurDir)) completeCFilePath(changeFileExt(gProjectFull, "js").prependCurDir))
execExternalProgram(findNodeJs() & " " & ex & ' ' & commands.arguments) 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: else:
var binPath: string var binPath: string
if options.outFile.len > 0: if options.outFile.len > 0:

View file

@ -70,7 +70,9 @@ type # please make sure we have under 32 options
TCommands* = enum # Nim's commands TCommands* = enum # Nim's commands
# **keep binary compatible** # **keep binary compatible**
cmdNone, cmdCompileToC, cmdCompileToCpp, cmdCompileToOC, cmdNone, cmdCompileToC, cmdCompileToCpp, cmdCompileToOC,
cmdCompileToJS, cmdCompileToLLVM, cmdInterpret, cmdPretty, cmdDoc, cmdCompileToJS,
cmdCompileToPHP,
cmdCompileToLLVM, cmdInterpret, cmdPretty, cmdDoc,
cmdGenDepend, cmdDump, cmdGenDepend, cmdDump,
cmdCheck, # semantic checking for whole project cmdCheck, # semantic checking for whole project
cmdParse, # parse a single file (for debugging) cmdParse, # parse a single file (for debugging)

View file

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

View file

@ -2452,6 +2452,9 @@ type
when defined(JS): when defined(JS):
proc add*(x: var string, y: cstring) {.asmNoStackFrame.} = proc add*(x: var string, y: cstring) {.asmNoStackFrame.} =
when defined(nimphp):
asm """`x` .= `y`;"""
else:
asm """ asm """
var len = `x`[0].length-1; var len = `x`[0].length-1;
for (var i = 0; i < `y`.length; ++i) { for (var i = 0; i < `y`.length; ++i) {

View file

@ -1,7 +1,7 @@
# #
# #
# Nim's Runtime Library # Nim's Runtime Library
# (c) Copyright 2012 Andreas Rumpf # (c) Copyright 2015 Andreas Rumpf
# #
# See the file "copying.txt", included in this # See the file "copying.txt", included in this
# distribution, for details about the copyright. # distribution, for details about the copyright.
@ -61,7 +61,6 @@ proc getCurrentExceptionMsg*(): string =
proc auxWriteStackTrace(f: PCallFrame): string = proc auxWriteStackTrace(f: PCallFrame): string =
type type
TempFrame = tuple[procname: cstring, line: int] TempFrame = tuple[procname: cstring, line: int]
{.deprecated: [TTempFrame: TempFrame].}
var var
it = f it = f
i = 0 i = 0
@ -128,7 +127,10 @@ proc reraiseException() {.compilerproc, asmNoStackFrame.} =
else: else:
when not defined(noUnhandledHandler): when not defined(noUnhandledHandler):
if excHandler == 0: if excHandler == 0:
var isNimException : bool var isNimException: bool
when defined(nimphp):
asm "`isNimException` = isset(`lastJSError`['m_type']);"
else:
asm "`isNimException` = lastJSError.m_type;" asm "`isNimException` = lastJSError.m_type;"
if isNimException: if isNimException:
unhandledException(cast[ref Exception](lastJSError)) unhandledException(cast[ref Exception](lastJSError))
@ -150,6 +152,22 @@ proc raiseFieldError(f: string) {.compilerproc, noreturn.} =
raise newException(FieldError, f & " is not accessible") raise newException(FieldError, f & " is not accessible")
proc SetConstr() {.varargs, asmNoStackFrame, compilerproc.} = proc SetConstr() {.varargs, asmNoStackFrame, compilerproc.} =
when defined(nimphp):
asm """
$args = func_get_args();
$result = array();
foreach ($args as $key=>$x) {
if (is_array(($val)) {
for ($j = $x[0]; $j <= $x[1]; $j++) {
result[$j] = true;
}
} else {
result[$x] = true;
}
}
return result;
"""
else:
asm """ asm """
var result = {}; var result = {};
for (var i = 0; i < arguments.length; ++i) { for (var i = 0; i < arguments.length; ++i) {
@ -166,6 +184,9 @@ proc SetConstr() {.varargs, asmNoStackFrame, compilerproc.} =
""" """
proc makeNimstrLit(c: cstring): string {.asmNoStackFrame, compilerproc.} = proc makeNimstrLit(c: cstring): string {.asmNoStackFrame, compilerproc.} =
when defined(nimphp):
{.emit: """return `c`;""".}
else:
{.emit: """ {.emit: """
var ln = `c`.length; var ln = `c`.length;
var result = new Array(ln + 1); var result = new Array(ln + 1);
@ -178,6 +199,9 @@ proc makeNimstrLit(c: cstring): string {.asmNoStackFrame, compilerproc.} =
""".} """.}
proc cstrToNimstr(c: cstring): string {.asmNoStackFrame, compilerproc.} = proc cstrToNimstr(c: cstring): string {.asmNoStackFrame, compilerproc.} =
when defined(nimphp):
{.emit: """return `c`;""".}
else:
{.emit: """ {.emit: """
var ln = `c`.length; var ln = `c`.length;
var result = new Array(ln); var result = new Array(ln);
@ -207,6 +231,9 @@ proc cstrToNimstr(c: cstring): string {.asmNoStackFrame, compilerproc.} =
""".} """.}
proc toJSStr(s: string): cstring {.asmNoStackFrame, compilerproc.} = proc toJSStr(s: string): cstring {.asmNoStackFrame, compilerproc.} =
when defined(nimphp):
{.emit: """return `s`;""".}
else:
asm """ asm """
var len = `s`.length-1; var len = `s`.length-1;
var asciiPart = new Array(len); var asciiPart = new Array(len);
@ -234,6 +261,13 @@ proc toJSStr(s: string): cstring {.asmNoStackFrame, compilerproc.} =
""" """
proc mnewString(len: int): string {.asmNoStackFrame, compilerproc.} = proc mnewString(len: int): string {.asmNoStackFrame, compilerproc.} =
when defined(nimphp):
asm """
$result = array();
for($i = 0; $i < `len`; $i++) $result[] = chr(0);
return $result;
"""
else:
asm """ asm """
var result = new Array(`len`+1); var result = new Array(`len`+1);
result[0] = 0; result[0] = 0;
@ -243,6 +277,11 @@ proc mnewString(len: int): string {.asmNoStackFrame, compilerproc.} =
proc SetCard(a: int): int {.compilerproc, asmNoStackFrame.} = proc SetCard(a: int): int {.compilerproc, asmNoStackFrame.} =
# argument type is a fake # argument type is a fake
when defined(nimphp):
asm """
return count(`a`);
"""
else:
asm """ asm """
var result = 0; var result = 0;
for (var elem in `a`) { ++result; } for (var elem in `a`) { ++result; }
@ -250,6 +289,13 @@ proc SetCard(a: int): int {.compilerproc, asmNoStackFrame.} =
""" """
proc SetEq(a, b: int): bool {.compilerproc, asmNoStackFrame.} = 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 """ asm """
for (var elem in `a`) { if (!`b`[elem]) return false; } for (var elem in `a`) { if (!`b`[elem]) return false; }
for (var elem in `b`) { if (!`a`[elem]) return false; } for (var elem in `b`) { if (!`a`[elem]) return false; }
@ -257,6 +303,12 @@ proc SetEq(a, b: int): bool {.compilerproc, asmNoStackFrame.} =
""" """
proc SetLe(a, b: int): bool {.compilerproc, asmNoStackFrame.} = 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 """ asm """
for (var elem in `a`) { if (!`b`[elem]) return false; } for (var elem in `a`) { if (!`b`[elem]) return false; }
return true; return true;
@ -266,6 +318,15 @@ proc SetLt(a, b: int): bool {.compilerproc.} =
result = SetLe(a, b) and not SetEq(a, b) result = SetLe(a, b) and not SetEq(a, b)
proc SetMul(a, b: int): int {.compilerproc, asmNoStackFrame.} = 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 """ asm """
var result = {}; var result = {};
for (var elem in `a`) { for (var elem in `a`) {
@ -275,6 +336,14 @@ proc SetMul(a, b: int): int {.compilerproc, asmNoStackFrame.} =
""" """
proc SetPlus(a, b: int): int {.compilerproc, asmNoStackFrame.} = 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 """ asm """
var result = {}; var result = {};
for (var elem in `a`) { result[elem] = true; } for (var elem in `a`) { result[elem] = true; }
@ -283,6 +352,15 @@ proc SetPlus(a, b: int): int {.compilerproc, asmNoStackFrame.} =
""" """
proc SetMinus(a, b: int): int {.compilerproc, asmNoStackFrame.} = 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 """ asm """
var result = {}; var result = {};
for (var elem in `a`) { for (var elem in `a`) {
@ -303,7 +381,15 @@ proc cmpStrings(a, b: string): int {.asmNoStackFrame, compilerProc.} =
return 0; return 0;
""" """
proc cmp(x, y: string): int = return cmpStrings(x, y) 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 eqStrings(a, b: string): bool {.asmNoStackFrame, compilerProc.} = proc eqStrings(a, b: string): bool {.asmNoStackFrame, compilerProc.} =
asm """ asm """
@ -338,7 +424,7 @@ elif defined(nodejs):
console.log(buf); console.log(buf);
""" """
else: elif not defined(nimphp):
proc ewriteln(x: cstring) = proc ewriteln(x: cstring) =
var node : JSRef var node : JSRef
{.emit: "`node` = document.getElementsByTagName('body')[0];".} {.emit: "`node` = document.getElementsByTagName('body')[0];".}
@ -364,6 +450,11 @@ else:
# Arithmetic: # Arithmetic:
proc addInt(a, b: int): int {.asmNoStackFrame, compilerproc.} = proc addInt(a, b: int): int {.asmNoStackFrame, compilerproc.} =
when defined(nimphp):
asm """
return `a` + `b`;
"""
else:
asm """ asm """
var result = `a` + `b`; var result = `a` + `b`;
if (result > 2147483647 || result < -2147483648) `raiseOverflow`(); if (result > 2147483647 || result < -2147483648) `raiseOverflow`();
@ -371,6 +462,11 @@ proc addInt(a, b: int): int {.asmNoStackFrame, compilerproc.} =
""" """
proc subInt(a, b: int): int {.asmNoStackFrame, compilerproc.} = proc subInt(a, b: int): int {.asmNoStackFrame, compilerproc.} =
when defined(nimphp):
asm """
return `a` - `b`;
"""
else:
asm """ asm """
var result = `a` - `b`; var result = `a` - `b`;
if (result > 2147483647 || result < -2147483648) `raiseOverflow`(); if (result > 2147483647 || result < -2147483648) `raiseOverflow`();
@ -378,6 +474,11 @@ proc subInt(a, b: int): int {.asmNoStackFrame, compilerproc.} =
""" """
proc mulInt(a, b: int): int {.asmNoStackFrame, compilerproc.} = proc mulInt(a, b: int): int {.asmNoStackFrame, compilerproc.} =
when defined(nimphp):
asm """
return `a` * `b`;
"""
else:
asm """ asm """
var result = `a` * `b`; var result = `a` * `b`;
if (result > 2147483647 || result < -2147483648) `raiseOverflow`(); if (result > 2147483647 || result < -2147483648) `raiseOverflow`();
@ -385,6 +486,11 @@ proc mulInt(a, b: int): int {.asmNoStackFrame, compilerproc.} =
""" """
proc divInt(a, b: int): int {.asmNoStackFrame, compilerproc.} = proc divInt(a, b: int): int {.asmNoStackFrame, compilerproc.} =
when defined(nimphp):
asm """
return floor(`a` / `b`);
"""
else:
asm """ asm """
if (`b` == 0) `raiseDivByZero`(); if (`b` == 0) `raiseDivByZero`();
if (`b` == -1 && `a` == 2147483647) `raiseOverflow`(); if (`b` == -1 && `a` == 2147483647) `raiseOverflow`();
@ -392,6 +498,11 @@ proc divInt(a, b: int): int {.asmNoStackFrame, compilerproc.} =
""" """
proc modInt(a, b: int): int {.asmNoStackFrame, compilerproc.} = proc modInt(a, b: int): int {.asmNoStackFrame, compilerproc.} =
when defined(nimphp):
asm """
return `a` % `b`;
"""
else:
asm """ asm """
if (`b` == 0) `raiseDivByZero`(); if (`b` == 0) `raiseDivByZero`();
if (`b` == -1 && `a` == 2147483647) `raiseOverflow`(); if (`b` == -1 && `a` == 2147483647) `raiseOverflow`();
@ -399,6 +510,11 @@ proc modInt(a, b: int): int {.asmNoStackFrame, compilerproc.} =
""" """
proc addInt64(a, b: int): int {.asmNoStackFrame, compilerproc.} = proc addInt64(a, b: int): int {.asmNoStackFrame, compilerproc.} =
when defined(nimphp):
asm """
return `a` + `b`;
"""
else:
asm """ asm """
var result = `a` + `b`; var result = `a` + `b`;
if (result > 9223372036854775807 if (result > 9223372036854775807
@ -407,6 +523,11 @@ proc addInt64(a, b: int): int {.asmNoStackFrame, compilerproc.} =
""" """
proc subInt64(a, b: int): int {.asmNoStackFrame, compilerproc.} = proc subInt64(a, b: int): int {.asmNoStackFrame, compilerproc.} =
when defined(nimphp):
asm """
return `a` - `b`;
"""
else:
asm """ asm """
var result = `a` - `b`; var result = `a` - `b`;
if (result > 9223372036854775807 if (result > 9223372036854775807
@ -415,6 +536,11 @@ proc subInt64(a, b: int): int {.asmNoStackFrame, compilerproc.} =
""" """
proc mulInt64(a, b: int): int {.asmNoStackFrame, compilerproc.} = proc mulInt64(a, b: int): int {.asmNoStackFrame, compilerproc.} =
when defined(nimphp):
asm """
return `a` * `b`;
"""
else:
asm """ asm """
var result = `a` * `b`; var result = `a` * `b`;
if (result > 9223372036854775807 if (result > 9223372036854775807
@ -423,6 +549,11 @@ proc mulInt64(a, b: int): int {.asmNoStackFrame, compilerproc.} =
""" """
proc divInt64(a, b: int): int {.asmNoStackFrame, compilerproc.} = proc divInt64(a, b: int): int {.asmNoStackFrame, compilerproc.} =
when defined(nimphp):
asm """
return floor(`a` / `b`);
"""
else:
asm """ asm """
if (`b` == 0) `raiseDivByZero`(); if (`b` == 0) `raiseDivByZero`();
if (`b` == -1 && `a` == 9223372036854775807) `raiseOverflow`(); if (`b` == -1 && `a` == 9223372036854775807) `raiseOverflow`();
@ -430,6 +561,11 @@ proc divInt64(a, b: int): int {.asmNoStackFrame, compilerproc.} =
""" """
proc modInt64(a, b: int): int {.asmNoStackFrame, compilerproc.} = proc modInt64(a, b: int): int {.asmNoStackFrame, compilerproc.} =
when defined(nimphp):
asm """
return `a` % `b`;
"""
else:
asm """ asm """
if (`b` == 0) `raiseDivByZero`(); if (`b` == 0) `raiseDivByZero`();
if (`b` == -1 && `a` == 9223372036854775807) `raiseOverflow`(); if (`b` == -1 && `a` == 9223372036854775807) `raiseOverflow`();
@ -587,6 +723,13 @@ proc genericReset(x: JSRef, ti: PNimType): JSRef {.compilerproc.} =
proc arrayConstr(len: int, value: JSRef, typ: PNimType): JSRef {. proc arrayConstr(len: int, value: JSRef, typ: PNimType): JSRef {.
asmNoStackFrame, compilerproc.} = asmNoStackFrame, compilerproc.} =
# types are fake # types are fake
when defined(nimphp):
asm """
$result = array();
for ($i = 0; $i < `len`; $i++) $result[] = `value`;
return $result;
"""
else:
asm """ asm """
var result = new Array(`len`); var result = new Array(`len`);
for (var i = 0; i < `len`; ++i) result[i] = nimCopy(null, `value`, `typ`); for (var i = 0; i < `len`; ++i) result[i] = nimCopy(null, `value`, `typ`);