Merge from mainline (167278:168000).
[official-gcc/graphite-test-results.git] / libgo / go / exp / ogle / rruntime.go
blob33f1935b89e0c6fd5ef881dcdbcb35978503c272
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 package ogle
7 import (
8 "debug/proc"
9 "exp/eval"
10 "reflect"
13 // This file contains remote runtime definitions. Using reflection,
14 // we convert all of these to interpreter types and layout their
15 // remote representations using the architecture rules.
17 // We could get most of these definitions from our own runtime
18 // package; however, some of them differ in convenient ways, some of
19 // them are not defined or exported by the runtime, and having our own
20 // definitions makes it easy to support multiple remote runtime
21 // versions. This may turn out to be overkill.
23 // All of these structures are prefixed with rt1 to indicate the
24 // runtime version and to mark them as types used only as templates
25 // for remote types.
28 * Runtime data headers
30 * See $GOROOT/src/pkg/runtime/runtime.h
33 type rt1String struct {
34 str uintptr
35 len int
38 type rt1Slice struct {
39 array uintptr
40 len int
41 cap int
44 type rt1Eface struct {
45 typ uintptr
46 ptr uintptr
50 * Runtime type structures
52 * See $GOROOT/src/pkg/runtime/type.h and $GOROOT/src/pkg/runtime/type.go
55 type rt1UncommonType struct {
56 name *string
57 pkgPath *string
58 //methods []method;
61 type rt1CommonType struct {
62 size uintptr
63 hash uint32
64 alg, align, fieldAlign uint8
65 string *string
66 uncommonType *rt1UncommonType
69 type rt1Type struct {
70 // While Type is technically an Eface, treating the
71 // discriminator as an opaque pointer and taking advantage of
72 // the commonType prologue on all Type's makes type parsing
73 // much simpler.
74 typ uintptr
75 ptr *rt1CommonType
78 type rt1StructField struct {
79 name *string
80 pkgPath *string
81 typ *rt1Type
82 tag *string
83 offset uintptr
86 type rt1StructType struct {
87 rt1CommonType
88 fields []rt1StructField
91 type rt1PtrType struct {
92 rt1CommonType
93 elem *rt1Type
96 type rt1SliceType struct {
97 rt1CommonType
98 elem *rt1Type
101 type rt1ArrayType struct {
102 rt1CommonType
103 elem *rt1Type
104 len uintptr
108 * Runtime scheduler structures
110 * See $GOROOT/src/pkg/runtime/runtime.h
113 // Fields beginning with _ are only for padding
115 type rt1Stktop struct {
116 stackguard uintptr
117 stackbase *rt1Stktop
118 gobuf rt1Gobuf
119 _args uint32
120 _fp uintptr
123 type rt1Gobuf struct {
124 sp uintptr
125 pc uintptr
126 g *rt1G
127 r0 uintptr
130 type rt1G struct {
131 _stackguard uintptr
132 stackbase *rt1Stktop
133 _defer uintptr
134 sched rt1Gobuf
135 _stack0 uintptr
136 _entry uintptr
137 alllink *rt1G
138 _param uintptr
139 status int16
140 // Incomplete
143 var rt1GStatus = runtimeGStatus{
144 Gidle: 0,
145 Grunnable: 1,
146 Grunning: 2,
147 Gsyscall: 3,
148 Gwaiting: 4,
149 Gmoribund: 5,
150 Gdead: 6,
153 // runtimeIndexes stores the indexes of fields in the runtime
154 // structures. It is filled in using reflection, so the name of the
155 // fields must match the names of the remoteType's in runtimeValues
156 // exactly and the names of the index fields must be the capitalized
157 // version of the names of the fields in the runtime structures above.
158 type runtimeIndexes struct {
159 String struct {
160 Str, Len int
162 Slice struct {
163 Array, Len, Cap int
165 Eface struct {
166 Typ, Ptr int
169 UncommonType struct {
170 Name, PkgPath int
172 CommonType struct {
173 Size, Hash, Alg, Align, FieldAlign, String, UncommonType int
175 Type struct {
176 Typ, Ptr int
178 StructField struct {
179 Name, PkgPath, Typ, Tag, Offset int
181 StructType struct {
182 Fields int
184 PtrType struct {
185 Elem int
187 SliceType struct {
188 Elem int
190 ArrayType struct {
191 Elem, Len int
194 Stktop struct {
195 Stackguard, Stackbase, Gobuf int
197 Gobuf struct {
198 Sp, Pc, G int
200 G struct {
201 Stackbase, Sched, Status, Alllink int
205 // Values of G status codes
206 type runtimeGStatus struct {
207 Gidle, Grunnable, Grunning, Gsyscall, Gwaiting, Gmoribund, Gdead int64
210 // runtimeValues stores the types and values that correspond to those
211 // in the remote runtime package.
212 type runtimeValues struct {
213 // Runtime data headers
214 String, Slice, Eface *remoteType
215 // Runtime type structures
216 Type, CommonType, UncommonType, StructField, StructType, PtrType,
217 ArrayType, SliceType *remoteType
218 // Runtime scheduler structures
219 Stktop, Gobuf, G *remoteType
220 // Addresses of *runtime.XType types. These are the
221 // discriminators on the runtime.Type interface. We use local
222 // reflection to fill these in from the remote symbol table,
223 // so the names must match the runtime names.
224 PBoolType,
225 PUint8Type, PUint16Type, PUint32Type, PUint64Type, PUintType, PUintptrType,
226 PInt8Type, PInt16Type, PInt32Type, PInt64Type, PIntType,
227 PFloat32Type, PFloat64Type, PFloatType,
228 PArrayType, PStringType, PStructType, PPtrType, PFuncType,
229 PInterfaceType, PSliceType, PMapType, PChanType,
230 PDotDotDotType, PUnsafePointerType proc.Word
231 // G status values
232 runtimeGStatus
235 // fillRuntimeIndexes fills a runtimeIndexes structure will the field
236 // indexes gathered from the remoteTypes recorded in a runtimeValues
237 // structure.
238 func fillRuntimeIndexes(runtime *runtimeValues, out *runtimeIndexes) {
239 outv := reflect.Indirect(reflect.NewValue(out)).(*reflect.StructValue)
240 outt := outv.Type().(*reflect.StructType)
241 runtimev := reflect.Indirect(reflect.NewValue(runtime)).(*reflect.StructValue)
243 // out contains fields corresponding to each runtime type
244 for i := 0; i < outt.NumField(); i++ {
245 // Find the interpreter type for this runtime type
246 name := outt.Field(i).Name
247 et := runtimev.FieldByName(name).Interface().(*remoteType).Type.(*eval.StructType)
249 // Get the field indexes of the interpreter struct type
250 indexes := make(map[string]int, len(et.Elems))
251 for j, f := range et.Elems {
252 if f.Anonymous {
253 continue
255 name := f.Name
256 if name[0] >= 'a' && name[0] <= 'z' {
257 name = string(name[0]+'A'-'a') + name[1:]
259 indexes[name] = j
262 // Fill this field of out
263 outStructv := outv.Field(i).(*reflect.StructValue)
264 outStructt := outStructv.Type().(*reflect.StructType)
265 for j := 0; j < outStructt.NumField(); j++ {
266 f := outStructv.Field(j).(*reflect.IntValue)
267 name := outStructt.Field(j).Name
268 f.Set(int64(indexes[name]))