github.com/tardisgo/tardisgo@v0.0.0-20161119180838-e0dd9a7e46b5/haxe/offsets.go (about)

     1  // Copyright 2014 Elliott Stoneham and The TARDIS Go Authors
     2  // Use of this source code is governed by an MIT-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package haxe
     6  
     7  import (
     8  	"fmt"
     9  	"go/types"
    10  )
    11  
    12  var haxeStdSizes = types.StdSizes{
    13  	WordSize: 4, // word size in bytes - must be >= 4 (32bits)
    14  	MaxAlign: 8, // maximum alignment in bytes - must be >= 1
    15  }
    16  
    17  func fieldOffset(str *types.Struct, fldNum int) int64 {
    18  	fieldList := make([]*types.Var, str.NumFields())
    19  	for f := 0; f < str.NumFields(); f++ {
    20  		fieldList[f] = str.Field(f)
    21  	}
    22  	return haxeStdSizes.Offsetsof(fieldList)[fldNum]
    23  }
    24  
    25  func arrayOffsetCalc(ele types.Type) string {
    26  	ent := types.NewVar(0, nil, "___temp", ele)
    27  	fieldList := []*types.Var{ent, ent}
    28  	off := haxeStdSizes.Offsetsof(fieldList)[1] // to allow for word alignment
    29  	//off := haxeStdSizes.Sizeof(ele) // ?? or should it be the code above ?
    30  	if off == 1 {
    31  		return ""
    32  	}
    33  	for ls := uint(1); ls < 31; ls++ {
    34  		target := int64(1 << ls)
    35  		if off == target {
    36  			return fmt.Sprintf("<<%d", ls)
    37  		}
    38  		if off < target {
    39  			break // no point in looking any further
    40  		}
    41  	}
    42  	return fmt.Sprintf("*%d", off)
    43  }