diff --git a/compiler.go b/compiler.go index 1a2094e3..7d52ef06 100644 --- a/compiler.go +++ b/compiler.go @@ -1016,8 +1016,22 @@ func (c *Compiler) getInterpretedValue(value Value) (llvm.Value, error) { return llvm.Value{}, nil } - keyString := constant.StringVal(key.(*ConstValue).Expr.Value) - hash := stringhash(&keyString) + constVal := key.(*ConstValue).Expr + var keyBuf []byte + switch constVal.Type().Underlying().(*types.Basic).Kind() { + case types.String: + keyBuf = []byte(constant.StringVal(constVal.Value)) + case types.Int: + keyBuf = make([]byte, c.targetData.TypeAllocSize(c.intType)) + n, _ := constant.Uint64Val(constVal.Value) + for i := range keyBuf { + keyBuf[i] = byte(n) + n >>= 8 + } + default: + return llvm.Value{}, errors.New("todo: init: map key not implemented: " + constVal.Type().Underlying().String()) + } + hash := hashmapHash(keyBuf) if i%8 == 0 && i != 0 { // Bucket is full, create a new one. diff --git a/util.go b/util.go index 800b463d..499027a6 100644 --- a/util.go +++ b/util.go @@ -6,10 +6,10 @@ package main // Get FNV-1a hash of this string. // // https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function#FNV-1a_hash -func stringhash(s *string) uint32 { +func hashmapHash(data []byte) uint32 { var result uint32 = 2166136261 // FNV offset basis - for i := 0; i < len(*s); i++ { - result ^= uint32((*s)[i]) + for _, c := range data { + result ^= uint32(c) result *= 16777619 // FNV prime } return result