Skip to content

Commit

Permalink
tidy code and move run-related globals to context structs
Browse files Browse the repository at this point in the history
(only regression tested on haxe/JS)
  • Loading branch information
elliott5 committed Aug 4, 2015
1 parent e68e5f3 commit e0062ae
Show file tree
Hide file tree
Showing 24 changed files with 1,319 additions and 1,284 deletions.
484 changes: 242 additions & 242 deletions goroot/haxe/go1.4/src/tgotests.log

Large diffs are not rendered by default.

556 changes: 263 additions & 293 deletions haxe/base.go

Large diffs are not rendered by default.

90 changes: 45 additions & 45 deletions haxe/goclass.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,25 +78,25 @@ func (l langType) GoClassEnd(pkg *ssa.Package) string {
pos := "public static function CPos(pos:Int):String {\nvar prefix:String=\"\";\n"
pos += fmt.Sprintf(`if (pos==%d) return "(pogo.NoPosHash)";`, pogo.NoPosHash) + "\n"
pos += "if (pos<0) { pos = -pos; prefix= \"near \";}\n"
for p := len(pogo.PosHashFileList) - 1; p >= 0; p-- {
for p := len(l.PogoComp().PosHashFileList) - 1; p >= 0; p-- {
pos += fmt.Sprintf(`if(pos>%d) return prefix+"%s:"+Std.string(pos-%d);`,
pogo.PosHashFileList[p].BasePosHash,
strings.Replace(pogo.PosHashFileList[p].FileName, "\\", "\\\\", -1),
pogo.PosHashFileList[p].BasePosHash) + "\n"
l.PogoComp().PosHashFileList[p].BasePosHash,
strings.Replace(l.PogoComp().PosHashFileList[p].FileName, "\\", "\\\\", -1),
l.PogoComp().PosHashFileList[p].BasePosHash) + "\n"
}
pos += "return \"(invalid pogo.PosHash:\"+Std.string(pos)+\")\";\n}\n"

if pogo.DebugFlag {
if l.PogoComp().DebugFlag {
pos += "\npublic static function getStartCPos(s:String):Int {\n"
for p := len(pogo.PosHashFileList) - 1; p >= 0; p-- {
for p := len(l.PogoComp().PosHashFileList) - 1; p >= 0; p-- {
pos += "\t" + fmt.Sprintf(`if("%s".indexOf(s)!=-1) return %d;`,
strings.Replace(pogo.PosHashFileList[p].FileName, "\\", "\\\\", -1),
pogo.PosHashFileList[p].BasePosHash) + "\n"
strings.Replace(l.PogoComp().PosHashFileList[p].FileName, "\\", "\\\\", -1),
l.PogoComp().PosHashFileList[p].BasePosHash) + "\n"
}
pos += "\treturn -1;\n}\n"

pos += "\npublic static function getGlobal(s:String):String {\n"
globs := pogo.GlobalList()
globs := l.PogoComp().GlobalList()
for _, g := range globs {
goName := strings.Replace(g.Package+"."+g.Member, "\\", "\\\\", -1)
pos += "\t" + fmt.Sprintf(`if("%s".indexOf(s)!=-1) return "%s = "+%s.toString();`,
Expand All @@ -109,10 +109,10 @@ func (l langType) GoClassEnd(pkg *ssa.Package) string {
return main + pos + "} // end Go class"
}

func haxeStringConst(sconst string, position string) string {
func (l langType) haxeStringConst(sconst string, position string) string {
s, err := strconv.Unquote(sconst)
if err != nil {
pogo.LogError(position, "Haxe", errors.New(err.Error()+" : "+sconst))
l.PogoComp().LogError(position, "Haxe", errors.New(err.Error()+" : "+sconst))
return ""
}
ret0 := ""
Expand Down Expand Up @@ -161,15 +161,15 @@ func haxeStringConst(sconst string, position string) string {
return ` #if (cpp || neko || php) ` + ret0 + ` #else ` + ret + " #end "
}

func constFloat64(lit ssa.Const, bits int, position string) string {
func (l langType) constFloat64(lit ssa.Const, bits int, position string) string {
var f float64
var f32 float32
f, _ /*f64ok*/ = exact.Float64Val(lit.Value)
f32, _ /*f32ok*/ = exact.Float32Val(lit.Value)
if bits == 32 {
f = float64(f32)
}
haxeVal := pogo.FloatVal(lit.Value, bits, position)
haxeVal := l.PogoComp().FloatVal(lit.Value, bits, position)
switch {
case math.IsInf(f, +1):
haxeVal = "Math.POSITIVE_INFINITY"
Expand All @@ -183,7 +183,7 @@ func constFloat64(lit ssa.Const, bits int, position string) string {
return haxeVal
}

func (langType) Const(lit ssa.Const, position string) (typ, val string) {
func (l langType) Const(lit ssa.Const, position string) (typ, val string) {
if lit.Value == nil {
return "Dynamic", "null"
}
Expand All @@ -195,78 +195,78 @@ func (langType) Const(lit ssa.Const, position string) (typ, val string) {
// TODO check if conversion of some string constant declarations are required
switch lit.Type().Underlying().(type) {
case *types.Basic:
return "String", haxeStringConst(lit.Value.String(), position)
return "String", l.haxeStringConst(lit.Value.String(), position)
case *types.Slice:
return "Slice", "Force.toUTF8slice(this._goroutine," + haxeStringConst(lit.Value.String(), position) + ")"
return "Slice", "Force.toUTF8slice(this._goroutine," + l.haxeStringConst(lit.Value.String(), position) + ")"
default:
pogo.LogError(position, "Haxe", fmt.Errorf("haxe.Const() internal error, unknown string type"))
l.PogoComp().LogError(position, "Haxe", fmt.Errorf("haxe.Const() internal error, unknown string type"))
}
case exact.Float:
switch lit.Type().Underlying().(*types.Basic).Kind() {
case types.Float32:
return "Float", constFloat64(lit, 32, position)
return "Float", l.constFloat64(lit, 32, position)
case types.Float64, types.UntypedFloat:
return "Float", constFloat64(lit, 64, position)
return "Float", l.constFloat64(lit, 64, position)
case types.Complex64:
return "Complex", fmt.Sprintf("new Complex(%s,0)", pogo.FloatVal(lit.Value, 32, position))
return "Complex", fmt.Sprintf("new Complex(%s,0)", l.PogoComp().FloatVal(lit.Value, 32, position))
case types.Complex128:
return "Complex", fmt.Sprintf("new Complex(%s,0)", pogo.FloatVal(lit.Value, 64, position))
return "Complex", fmt.Sprintf("new Complex(%s,0)", l.PogoComp().FloatVal(lit.Value, 64, position))
}
case exact.Int:
h, l := pogo.IntVal(lit.Value, position)
hi, lo := l.PogoComp().IntVal(lit.Value, position)
switch lit.Type().Underlying().(*types.Basic).Kind() {
case types.Int64:
return "GOint64", fmt.Sprintf("Force.toInt64(GOint64.make(0x%x,0x%x))", uint32(h), uint32(l))
return "GOint64", fmt.Sprintf("Force.toInt64(GOint64.make(0x%x,0x%x))", uint32(hi), uint32(lo))
case types.Uint64:
return "GOint64", fmt.Sprintf("Force.toUint64(GOint64.make(0x%x,0x%x))", uint32(h), uint32(l))
return "GOint64", fmt.Sprintf("Force.toUint64(GOint64.make(0x%x,0x%x))", uint32(hi), uint32(lo))
case types.Float32:
return "Float", constFloat64(lit, 32, position)
return "Float", l.constFloat64(lit, 32, position)
case types.Float64, types.UntypedFloat:
return "Float", constFloat64(lit, 64, position)
return "Float", l.constFloat64(lit, 64, position)
case types.Complex64:
return "Complex", fmt.Sprintf("new Complex(%s,0)", pogo.FloatVal(lit.Value, 32, position))
return "Complex", fmt.Sprintf("new Complex(%s,0)", l.PogoComp().FloatVal(lit.Value, 32, position))
case types.Complex128:
return "Complex", fmt.Sprintf("new Complex(%s,0)", pogo.FloatVal(lit.Value, 64, position))
return "Complex", fmt.Sprintf("new Complex(%s,0)", l.PogoComp().FloatVal(lit.Value, 64, position))
default:
if h != 0 && h != -1 {
pogo.LogWarning(position, "Haxe", fmt.Errorf("integer constant value > 32 bits : %v", lit.Value))
if hi != 0 && hi != -1 {
l.PogoComp().LogWarning(position, "Haxe", fmt.Errorf("integer constant value > 32 bits : %v", lit.Value))
}
ret := ""
switch lit.Type().Underlying().(*types.Basic).Kind() {
case types.Uint, types.Uint32, types.Uintptr:
q := uint32(l)
q := uint32(lo)
ret = fmt.Sprintf(
" #if js untyped __js__(\"0x%x\") #elseif php untyped __php__(\"0x%x\") #else 0x%x #end ",
q, q, q)
case types.Uint16:
q := uint16(l)
q := uint16(lo)
ret = fmt.Sprintf(" 0x%x ", q)
case types.Uint8: // types.Byte
q := uint8(l)
q := uint8(lo)
ret = fmt.Sprintf(" 0x%x ", q)
case types.Int, types.Int32, types.UntypedRune, types.UntypedInt: // types.Rune
if l < 0 {
ret = fmt.Sprintf("(%d)", int32(l))
if lo < 0 {
ret = fmt.Sprintf("(%d)", int32(lo))
} else {
ret = fmt.Sprintf("%d", int32(l))
ret = fmt.Sprintf("%d", int32(lo))
}
case types.Int16:
if l < 0 {
ret = fmt.Sprintf("(%d)", int16(l))
if lo < 0 {
ret = fmt.Sprintf("(%d)", int16(lo))
} else {
ret = fmt.Sprintf("%d", int16(l))
ret = fmt.Sprintf("%d", int16(lo))
}
case types.Int8:
if l < 0 {
ret = fmt.Sprintf("(%d)", int8(l))
if lo < 0 {
ret = fmt.Sprintf("(%d)", int8(lo))
} else {
ret = fmt.Sprintf("%d", int8(l))
ret = fmt.Sprintf("%d", int8(lo))
}
case types.UnsafePointer:
if l == 0 {
if lo == 0 {
return "Pointer", "null"
}
pogo.LogError(position, "Haxe", fmt.Errorf("unsafe pointers cannot be initialized in TARDISgo/Haxe to a non-zero value: %v", l))
l.PogoComp().LogError(position, "Haxe", fmt.Errorf("unsafe pointers cannot be initialized in TARDISgo/Haxe to a non-zero value: %v", lo))
default:
panic("haxe.Const() unhandled integer constant for: " +
lit.Type().Underlying().(*types.Basic).String())
Expand All @@ -285,7 +285,7 @@ func (langType) Const(lit ssa.Const, position string) (typ, val string) {
return "Complex", fmt.Sprintf("new Complex(%g,%g)", realV, imagV)
}
}
pogo.LogError(position, "Haxe", fmt.Errorf("haxe.Const() internal error, unknown constant type: %v", lit.Value.Kind()))
l.PogoComp().LogError(position, "Haxe", fmt.Errorf("haxe.Const() internal error, unknown constant type: %v", lit.Value.Kind()))
return "", ""
}

Expand Down
44 changes: 21 additions & 23 deletions haxe/haxeRuntime.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

package haxe

import "github.com/tardisgo/tardisgo/pogo"

// Runtime Haxe code for Go, which may eventually become a haxe library when the system settles down.
// TODO All runtime class names are currently carried through if the haxe code uses "import tardis.Go;" and some are too generic,
// others, like Int64, will overload the Haxe standard library version for some platforms, which may cause other problems.
Expand All @@ -14,9 +12,9 @@ import "github.com/tardisgo/tardisgo/pogo"
// However, there are references to Go->Haxe generated classes, like "Go", that would need to be managed somehow.
// TODO consider merging and possibly renaming the Deep and Force classes as they both hold general utility code

func haxeruntime() string {
func (l langType) haxeruntime() string {

pogo.WriteAsClass("Console", `
l.PogoComp().WriteAsClass("Console", `
class Console {
public static inline function naclWrite(v:String){
Expand Down Expand Up @@ -78,7 +76,7 @@ class Console {
}
`)
pogo.WriteAsClass("Force", `
l.PogoComp().WriteAsClass("Force", `
// TODO: consider putting these go-compatibiliy classes into a separate library for general Haxe use when calling Go
class Force { // TODO maybe this should not be a separate haxe class, as no non-Go code needs access to it
Expand Down Expand Up @@ -796,7 +794,7 @@ class Object {
public static function objBlit(src:Object,srcPos:Int,dest:Object,destPos:Int,size:Int):Void{
if(size>0&&src!=null) {
`
if pogo.DebugFlag {
if l.PogoComp().DebugFlag {
objClass += `
#if !abstractobjects
if(!Std.is(src,Object)) {
Expand Down Expand Up @@ -1247,15 +1245,15 @@ class Object {
}
}
`
pogo.WriteAsClass("Object", objClass)
l.PogoComp().WriteAsClass("Object", objClass)

ptrClass := `
@:keep
class Pointer {
public var obj:Object; // reference to the object holding the value
public var off:Int; // the offset into the object, if any
`
if pogo.DebugFlag {
if l.PogoComp().DebugFlag {
ptrClass += `
public function new(from:Object,offset:Int){
if(from==null) Scheduler.panicFromHaxe("attempt to make a new Pointer from a nil object");
Expand All @@ -1279,7 +1277,7 @@ class Pointer {
return r;
}
`
if pogo.DebugFlag {
if l.PogoComp().DebugFlag {
ptrClass += ` public static function check(p:Dynamic):Pointer {
if(p==null) {
Scheduler.panicFromHaxe("nil pointer de-reference");
Expand All @@ -1297,7 +1295,7 @@ class Pointer {
return p;
}`
}
pogo.WriteAsClass("Pointer", ptrClass+
l.PogoComp().WriteAsClass("Pointer", ptrClass+
` public static function isEqual(p1:Pointer,p2:Pointer):Bool {
if(p1==p2) return true; // simple case of being the same haxe object
if(p1==null || p2==null) return false; // one of them is null (if above handles both null)
Expand Down Expand Up @@ -1584,7 +1582,7 @@ class Slice {
return capacity-start;
}
`
if pogo.DebugFlag { // Normal range checking should cover this, so only in debug mode
if l.PogoComp().DebugFlag { // Normal range checking should cover this, so only in debug mode
sliceClass += `
public function itemAddr(idx:Int):Pointer {
if (idx<0 || idx>=len())
Expand Down Expand Up @@ -1621,8 +1619,8 @@ class Slice {
}
}
`
pogo.WriteAsClass("Slice", sliceClass)
pogo.WriteAsClass("Closure", `
l.PogoComp().WriteAsClass("Slice", sliceClass)
l.PogoComp().WriteAsClass("Closure", `
@:keep
class Closure { // "closure" is a keyword in PHP but solved using compiler flag --php-prefix go //TODO tidy names
Expand Down Expand Up @@ -1683,7 +1681,7 @@ class Closure { // "closure" is a keyword in PHP but solved using compiler flag
}
}
`)
pogo.WriteAsClass("Interface", `
l.PogoComp().WriteAsClass("Interface", `
class Interface { // "interface" is a keyword in PHP but solved using compiler flag --php-prefix tgo //TODO tidy names
public var typ:Int; // the possibly interface type that has been cast to
Expand Down Expand Up @@ -1833,7 +1831,7 @@ class Interface { // "interface" is a keyword in PHP but solved using compiler f
}
}
`)
pogo.WriteAsClass("Channel", `
l.PogoComp().WriteAsClass("Channel", `
class Channel { // NOTE single-threaded implementation, no locking
var entries:Array<Dynamic>;
Expand Down Expand Up @@ -1915,7 +1913,7 @@ public function toString():String{
}
}
`)
pogo.WriteAsClass("Complex", `
l.PogoComp().WriteAsClass("Complex", `
class Complex {
public var real:Float;
Expand Down Expand Up @@ -1958,7 +1956,7 @@ public static function toString(x:Complex):String {
}
`)
pogo.WriteAsClass("GOint64", `
l.PogoComp().WriteAsClass("GOint64", `
#if ( neko || cpp || cs || java )
typedef HaxeInt64Typedef = haxe.Int64; // these implementations are using native types
Expand Down Expand Up @@ -2523,7 +2521,7 @@ class Int64 {
//**************** END REWRITE of haxe.Int64 for php and to correct errors
`)
pogo.WriteAsClass("StackFrameBasis", `
l.PogoComp().WriteAsClass("StackFrameBasis", `
// GoRoutine
class StackFrameBasis
Expand Down Expand Up @@ -2751,7 +2749,7 @@ public function runDefers(){
}
`)
pogo.WriteAsClass("StackFrame", `
l.PogoComp().WriteAsClass("StackFrame", `
interface StackFrame
{
Expand All @@ -2772,7 +2770,7 @@ function nullOnExitSF():Void; // call this when exiting the function
function setDebugVar(name:String,value:Dynamic):Void;
}
`)
pogo.WriteAsClass("Scheduler", `
l.PogoComp().WriteAsClass("Scheduler", `
@:cppFileCode('extern "C" int tardisgo_timereventhandler(int rl) { tardis::Scheduler_obj::runLimit=rl; tardis::Scheduler_obj::timerEventHandler(0); return 0; }')
Expand Down Expand Up @@ -3092,7 +3090,7 @@ public static function wrapnilchk(p:Pointer):Pointer {
}
}
`)
pogo.WriteAsClass("GOmap", `
l.PogoComp().WriteAsClass("GOmap", `
class GOmap {
// TODO write a more sophisticated (and hopefully faster) version of this code
Expand Down Expand Up @@ -3212,7 +3210,7 @@ class GOmap {
}
`)
pogo.WriteAsClass("GOmapRange", `
l.PogoComp().WriteAsClass("GOmapRange", `
class GOmapRange {
private var k:Array<String>;
Expand All @@ -3237,7 +3235,7 @@ class GOmapRange {
}
}
`)
pogo.WriteAsClass("GOstringRange", `
l.PogoComp().WriteAsClass("GOstringRange", `
class GOstringRange {
private var g:Int;
Expand Down
Loading

0 comments on commit e0062ae

Please sign in to comment.