示例#1
0
//
// type-check value of type tid, about to be copied,
// and return its size.
//
func tchk(t *Tent, s interface{}) uint {
	var (
		ep int
		f  float32
	)
	switch t.fmt {
	case 'b', 'c', 'i', 'e', 'h', 'z', 'u':
		switch s.(type) {
		case []byte:
			eep, err := pbytes.UnmarshalBinary(s.([]byte), int32(ep))
			if err != nil {
				panic("tchk umarshal")
			}
			ep = int(eep.(int32))
		case *int:
			ep = *s.(*int)
		default:
			panic("wrong type in tchk")
		}
		if ep<t.first || ep>t.last {
			panic("assigned value out of range")
		}
	case 'l':
		ifc, err := pbytes.UnmarshalBinary(mst.stack[mst.sp:], f)
		if err != nil {
			panic("tchk float")
		}
		f := ifc.(float32)
		if f<float32(t.first) || f>float32(t.last) {
			panic("assigned value out of range")
		}
	case 'p':
		// check pointer value
	case 'a':
		//
		// Arrays may be used as buffer space,
		// and it is not reasonable to require the
		// entire array to be ok regarding values.
		// So, disable this check.
		//
	case 'R':
		cp := s.([]byte) //TODO offsets, etc, see original picky and reslice
		for i := 0; i < t.nitems; i++ {
			nt := &mabs.tents[t.fields[i].tid]
			tchk(nt, cp[0:int(nt.sz)])
			cp = cp[nt.sz:]
		}
	}
	return t.sz
}
示例#2
0
func ptrU64(p *byte) (u uint64) {
	ps := sliceptr(uintptr(unsafe.Pointer(p)), p64sz)
	ifc, err := pbytes.UnmarshalBinary(ps, uintptr(unsafe.Pointer(p)))
	if err != nil {
		panic("ptrPtr marshal")
	}
	return ifc.(uint64)
}
示例#3
0
func ptrU32(p *byte) (u uint32) {
	ps := sliceptr(uintptr(unsafe.Pointer(p)), p32sz)
	ifc, err := pbytes.UnmarshalBinary(ps, u)
	if err != nil {
		panic("ptrPtr marshal")
	}
	u = ifc.(uint32)
	return u
}
示例#4
0
func ptrPtr(p uintptr) (pt *Ptr) {
	if unsafe.Sizeof(pt) > p64sz {
		panic("pointers are too big")
	}
	ps := sliceptr(p, p64sz)
	ifc, err := pbytes.UnmarshalBinary(ps, p)
	if err != nil {
		panic("ptrPtr marshal")
	}
	return (*Ptr)(unsafe.Pointer(ifc.(uintptr)))
}
示例#5
0
func top64() uint64 {
	var (
		n   uint64
		err error
		ifc interface{}
	)
	if mst.sp < 8 {
		panic("stack underflow")
	}
	ifc, err = pbytes.UnmarshalBinary(mst.stack[mst.sp:], n)
	if err != nil {
		panic("top64 unmarshal")
	}
	n = ifc.(uint64)
	return n
}
示例#6
0
func top32() uint32 {
	var (
		n   uint32
		err error
		ifc interface{}
	)
	if mst.sp < 4 {
		panic("stack underflow")
	}
	ifc, err = pbytes.UnmarshalBinary(mst.stack[mst.sp:], n)
	if err != nil {
		panic("top32 unmarshal")
	}
	n = ifc.(uint32)
	return n
}
示例#7
0
func topr() float64 {
	var (
		n   float32
		err error
		ifc interface{}
	)
	if mst.sp < 4 {
		panic("stack underflow")
	}
	ifc, err = pbytes.UnmarshalBinary(mst.stack[mst.sp:], n)
	if err != nil {
		panic("topr unmarshal")
	}
	n = ifc.(float32)
	c := float64(n)
	if math.IsNaN(c) {
		panic("invalid operation on float")
	}
	return c
}
示例#8
0
func popn(sz int) string {
	var (
		rv  rune
		err error
		ifc interface{}
	)
	r := make([]byte, sz)
	if mst.sp < sz {
		panic("stack underflow")
	}
	mst.sp -= sz
	ifc, err = pbytes.UnmarshalBinary(mst.stack[mst.sp:mst.sp+sz], r)
	if err != nil {
		panic("popn unmarshal")
	}
	p := ifc.([]byte)
	s := make([]byte, 0)
	for i := 0; i < sz; i += 4 {
		rv, _ = utf8.DecodeRune(p[i : i+4])
		s = append(s, byte(rv))
	}
	return string(s)
}
示例#9
0
func (v *Vmem) String() string {
	var (
		nv  Vmem
		err error
		ifc interface{}
	)
	t := &mabs.tents[v.tid]
	s := ""
	switch t.fmt {
	case 'b':
		var e uint32
		ifc, err = pbytes.UnmarshalBinary(v.elem, e)
		if err != nil {
			panic("vmem umarshal")
		}
		e = ifc.(uint32)
		if e != 0 {
			s += fmt.Sprintf("True")
		} else {
			s += fmt.Sprintf("False")
		}
	case 'c':
		var e rune
		ifc, err = pbytes.UnmarshalBinary(v.elem, e)
		if err != nil {
			panic("vmem umarshal")
		}
		e = ifc.(rune)
		s += dumpc(e)
	case 'i':
		var e uint32
		ifc, err = pbytes.UnmarshalBinary(v.elem, e)
		if err != nil {
			panic("vmem umarshal")
		}
		e = ifc.(uint32)
		ee := int(int32(e))
		if ee<t.first || ee>t.last {
			s += fmt.Sprintf("out of range")
		} else {
			s += fmt.Sprintf("%d", ee)
		}
		break
	case 'e', 'h':
		var e int
		ifc, err = pbytes.UnmarshalBinary(v.elem, e)
		if err != nil {
			panic("vmem umarshal")
		}
		e = ifc.(int)
		if e<t.first || e>t.last {
			s += fmt.Sprintf("out of range")
		} else {
			s += fmt.Sprintf("%s", t.lits[e-t.first])
		}
	case 'r', 'l':
		var e float32
		ifc, err = pbytes.UnmarshalBinary(v.elem, e)
		if err != nil {
			panic("vmem umarshal")
		}
		e = ifc.(float32)
		s += floatfmt(float64(e))
	case 'p':
		var e uintptr
		ifc, err = pbytes.UnmarshalBinary(v.elem, e)
		if err != nil {
			panic("vmem umarshal")
		}
		e = ifc.(uintptr)
		if e != 0 {
			s += fmt.Sprintf("%#x", e)
		} else {
			s += "nil"
		}
	case 'a':
		if mabs.tents[t.etid].fmt == 'c' {
			var e string
			ifc, err = pbytes.UnmarshalBinary(v.elem, e)
			if err != nil {
				panic("vmem umarshal")
			}
			e = ifc.(string)
			s += fmt.Sprintf("\"")
			s += fmt.Sprintf("%s", e)
			s += fmt.Sprintf("\"")
			break
		}
		s += fmt.Sprintf("{\n")
		vlvl++

		nv.tid = int(t.etid)
		st := uint(0)
		for i := 0; i < t.nitems; i++ {
			s += tabs(vlvl)
			nv.elem = v.elem[st : st+mabs.tents[t.etid].sz]
			s += fmt.Sprintf("[%d] %v,\n", i, nv)
			st += mabs.tents[t.etid].sz
		}
		vlvl--
		s += tabs(vlvl)
		s += fmt.Sprintf("}")
	case 'R':
		s += fmt.Sprintf("{\n")
		vlvl++
		st := uint(0)
		for i := 0; i < t.nitems; i++ {
			fld := t.fields[i]
			nv.tid = int(fld.tid)
			nv.elem = v.elem[st : st+mabs.tents[nv.tid].sz]
			s += tabs(vlvl)
			s += fmt.Sprintf(".%s = %v,\n", fld.name, nv)
		}
		vlvl--
		s += tabs(vlvl)
		s += fmt.Sprintf("}")
	case 'X':
		s += fmt.Sprintf("<procedure>")
	case 'f':
		var e int
		ifc, err = pbytes.UnmarshalBinary(v.elem, e)
		if err != nil {
			panic("vmem umarshal")
		}
		e = ifc.(int)
		if e<0 || e>=len(files) {
			s += fmt.Sprintf("invalid file")
			return s
		}
		if files[e] == nil {
			s += fmt.Sprintf("closed file")
			return s
		}
		switch e {
		case 0:
			s += fmt.Sprintf("<stdin>")
			break
		case 1:
			s += fmt.Sprintf("<stdout>")
			break
		default:
			s += fmt.Sprintf("<file #%d %s>", e, files[e])
		}
	case 'F':
		s += fmt.Sprintf("<function>")
		break
	case 's':
		var e string
		ifc, err = pbytes.UnmarshalBinary(v.elem, e)
		if err != nil {
			panic("vmem umarshal")
		}
		e = ifc.(string)
		s += fmt.Sprintf("\"")
		s += fmt.Sprintf("%s", e)
		s += fmt.Sprintf("\"")
	default:
		errs := fmt.Sprintf("vfmt: fmt %x", t.fmt)
		panic(errs)
	}
	return s
}