/
runinfo.go
577 lines (542 loc) · 16.9 KB
/
runinfo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
package bondgo
import (
"fmt"
"strconv"
)
const (
REQ_REMOVE = uint8(0) + iota // Removes the resoure from the local used list, if associated with a global one the global is never removed
REQ_NEW // New variable request (and eventually attach to a global one)
REQ_ATTACH // Attach a local var to a global one
REQ_EXIT
)
const (
ANS_OK = uint8(0) + iota
ANS_FAIL
)
type VarCells []VarCell
type BondgoRuninfo struct {
Config *BondgoConfig // Global compiler config
Used_cells map[int]VarCells
IO []IOInfo
Channels []ChanInfo
SharedRAM []SharedRAMInfo
}
// IO topology
type IOInfo struct {
Global_id int
Inputs []int
Output int
}
// bondmachine channels
type ChanInfo struct {
Global_id int
Connected []int
Readers []int
Writers []int
}
// bondmachine shared RAM
type SharedRAMInfo struct {
Global_id int
Connected []int
}
type VarReq struct {
ReqType uint8
Processor_id int
Cell VarCell
}
type VarAns struct {
AnsType uint8
Cell VarCell
}
func (vr *VarReq) String() string {
result := ""
switch vr.ReqType {
case REQ_NEW:
result += "new "
case REQ_REMOVE:
result += "del "
}
result += "proc " + strconv.Itoa(vr.Processor_id) + " "
result += "cell " + vr.Cell.String()
// TODO Finish
return result
}
func (ri *BondgoRuninfo) Init_Runinfo(cfg *BondgoConfig) {
ri.Config = cfg
ri.Used_cells = make(map[int]VarCells)
ri.IO = make([]IOInfo, 0)
ri.Channels = make([]ChanInfo, 0)
ri.SharedRAM = make([]SharedRAMInfo, 0)
}
// This goroutine assign or frees used memory within a processor
func (ri *BondgoRuninfo) Var_assigner(req chan VarReq, resp chan VarAns, useditem chan UsageNotify, assignerdone chan bool) {
debug := ri.Config.Debug
busylist := ri.Used_cells
busychan := ri.Channels
busyio := ri.IO
VA:
for {
r := <-req
rtype := r.ReqType
rproc := r.Processor_id
rcell := r.Cell
if debug {
fmt.Println("\tList of resources in use: ", busylist)
fmt.Println("\tOperation: ", &r)
}
switch rtype {
case REQ_REMOVE:
switch rcell.Procobjtype {
case REGISTER, MEMORY:
if gent, _ := Type_from_string(ri.Config.Basic_type); Same_Type(rcell.Vtype, gent) {
if _, ok := busylist[rproc]; ok {
blist := busylist[rproc]
if i, ok := memused(r.Cell, blist); ok {
if i < len(blist)-1 {
blist = append(blist[:i], blist[i+1:]...)
} else {
blist = blist[:i]
}
busylist[rproc] = blist
resp <- VarAns{ANS_OK, r.Cell}
} else {
panic("Attempt to remove an unused Memory cell")
}
} else {
panic("Attempt to remove an unused Memory cell")
}
} else if gent, _ := Type_from_string("bool"); Same_Type(rcell.Vtype, gent) {
if _, ok := busylist[rproc]; ok {
blist := busylist[rproc]
if i, ok := memused(r.Cell, blist); ok {
if i < len(blist)-1 {
blist = append(blist[:i], blist[i+1:]...)
} else {
blist = blist[:i]
}
busylist[rproc] = blist
resp <- VarAns{ANS_OK, r.Cell}
} else {
panic("Attempt to remove an unused Memory cell")
}
} else {
panic("Attempt to remove an unused Memory cell")
}
} else {
panic("Allocator received a wrong type, this cannot happen. A bug is here")
}
case INPUT, OUTPUT, CHANNEL:
// TODO Check and make better
resp <- VarAns{ANS_OK, r.Cell}
}
case REQ_NEW:
switch rcell.Procobjtype {
case REGISTER:
if gent, _ := Type_from_string(ri.Config.Basic_type); Same_Type(rcell.Vtype, gent) {
created := false
if _, ok := busylist[rproc]; !ok {
vcells := make([]VarCell, 0)
busylist[rproc] = vcells
}
for i := 0; i < MAX_REGS; i++ {
guessed := VarCell{gent, REGISTER, i, 0, 0, 0, 0, 0}
present := false
for _, assigned := range busylist[rproc] {
if assigned.Procobjtype == guessed.Procobjtype && assigned.Id == guessed.Id {
present = true
break
}
}
if !present {
resp <- VarAns{ANS_OK, guessed}
useditem <- UsageNotify{TR_PROC, rproc, C_REGSIZE, S_NIL, i + 1}
busylist[rproc] = append(busylist[rproc], guessed)
created = true
break
}
}
if !created {
panic("Recursion function not allowed")
}
} else if gent, _ := Type_from_string("bool"); Same_Type(rcell.Vtype, gent) {
created := false
if _, ok := busylist[rproc]; !ok {
vcells := make([]VarCell, 0)
busylist[rproc] = vcells
}
for i := 0; i < MAX_REGS; i++ {
guessed := VarCell{gent, REGISTER, i, 0, 0, 0, 0, 0}
present := false
for _, assigned := range busylist[rproc] {
if assigned.Procobjtype == guessed.Procobjtype && assigned.Id == guessed.Id {
present = true
break
}
}
if !present {
resp <- VarAns{ANS_OK, guessed}
useditem <- UsageNotify{TR_PROC, rproc, C_REGSIZE, S_NIL, i + 1}
busylist[rproc] = append(busylist[rproc], guessed)
created = true
break
}
}
if !created {
panic("Recursion function not allowed")
}
} else {
panic("Allocator received a wrong type, this cannot happen. A bug is here")
}
case MEMORY:
if gent, _ := Type_from_string(ri.Config.Basic_type); Same_Type(rcell.Vtype, gent) {
created := false
if _, ok := busylist[rproc]; !ok {
vcells := make([]VarCell, 0)
busylist[rproc] = vcells
}
for i := 0; i < MAX_MEMORY; i++ {
// TODO This code uses only 1 memory area per variable, remember whenever it will happen that other types will be inserted that this code has to be substituted with something else
guessed := VarCell{gent, MEMORY, i, i, i, 0, 0, 0}
present := false
for _, assigned := range busylist[rproc] {
if assigned.Procobjtype == guessed.Procobjtype && assigned.Id == guessed.Id {
present = true
break
}
}
if !present {
resp <- VarAns{ANS_OK, guessed}
useditem <- UsageNotify{TR_PROC, rproc, C_RAMSIZE, S_NIL, i + 1}
busylist[rproc] = append(busylist[rproc], guessed)
created = true
break
}
}
if !created {
panic("Recursion function not allowed")
}
} else if gent, _ := Type_from_string("bool"); Same_Type(rcell.Vtype, gent) {
created := false
if _, ok := busylist[rproc]; !ok {
vcells := make([]VarCell, 0)
busylist[rproc] = vcells
}
for i := 0; i < MAX_MEMORY; i++ {
// TODO This code uses only 1 memory area per variable, remember whenever it will happen that other types will be inserted that this code has to be substituted with something else
guessed := VarCell{gent, MEMORY, i, i, i, 0, 0, 0}
present := false
for _, assigned := range busylist[rproc] {
if assigned.Procobjtype == guessed.Procobjtype && assigned.Id == guessed.Id {
present = true
break
}
}
if !present {
resp <- VarAns{ANS_OK, guessed}
useditem <- UsageNotify{TR_PROC, rproc, C_RAMSIZE, S_NIL, i + 1}
busylist[rproc] = append(busylist[rproc], guessed)
created = true
break
}
}
if !created {
panic("Recursion function not allowed")
}
} else {
panic("Allocator received a wrong type, this cannot happen. A bug is here")
}
case INPUT:
if gent, _ := Type_from_string(ri.Config.Basic_type); Same_Type(rcell.Vtype, gent) {
if _, ok := busylist[rproc]; !ok {
vcells := make([]VarCell, 0)
busylist[rproc] = vcells
}
// Check for IO consistency (Only if the IO is inittializated) and global data update
consistent := true
globalpresent := false
if rcell.Global_id != 0 {
for _, ioinfo := range busyio {
if ioinfo.Global_id == rcell.Global_id {
globalpresent = true
if ioinfo.Output == rproc {
// A processor cannot have and input connected to an output
consistent = false
break
} else {
ioinfo.Inputs = append(ioinfo.Inputs, rproc)
}
}
}
if !globalpresent {
newioinputs := make([]int, 1)
newioinputs[0] = rproc
newio := IOInfo{rcell.Global_id, newioinputs, 0}
busyio = append(busyio, newio)
}
}
if consistent {
for i := 0; i < MAX_INPUTS; i++ {
guessed := VarCell{gent, INPUT, i, i, i, rcell.Global_id, rcell.Start_globalid, rcell.End_globalid}
present := false
for _, assigned := range busylist[rproc] {
if assigned.Procobjtype == guessed.Procobjtype && assigned.Id == guessed.Id {
present = true
break
}
}
if !present {
resp <- VarAns{ANS_OK, guessed}
// Only in the IO is inittializated its use has to be notified
if rcell.Global_id != 0 {
useditem <- UsageNotify{TR_PROC, rproc, C_INPUT, S_NIL, rcell.Global_id}
}
busylist[rproc] = append(busylist[rproc], guessed)
break
}
}
} else {
resp <- VarAns{ANS_FAIL, VarCell{gent, 0, 0, 0, 0, 0, 0, 0}}
}
} else {
panic("Allocator received a wrong type, this cannot happen. A bug is here")
}
case OUTPUT:
if gent, _ := Type_from_string(ri.Config.Basic_type); Same_Type(rcell.Vtype, gent) {
if _, ok := busylist[rproc]; !ok {
vcells := make([]VarCell, 0)
busylist[rproc] = vcells
}
// Check for IO consistency (Only if the IO is inittializated)
consistent := true
globalpresent := false
if rcell.Global_id != 0 {
for _, ioinfo := range busyio {
if ioinfo.Global_id == rcell.Global_id {
globalpresent = true
for _, ioimp := range ioinfo.Inputs {
if ioimp == rproc {
// A processor cannot have and input connected to an output
consistent = false
break
}
}
if ioinfo.Output != 0 {
// There can be only one output for every global id
consistent = false
break
} else {
ioinfo.Output = rproc
}
}
}
if !globalpresent {
newioinputs := make([]int, 0)
newio := IOInfo{rcell.Global_id, newioinputs, rproc}
busyio = append(busyio, newio)
}
}
if consistent {
for i := 0; i < MAX_OUTPUTS; i++ {
guessed := VarCell{gent, OUTPUT, i, i, i, rcell.Global_id, rcell.Start_globalid, rcell.End_globalid}
present := false
for _, assigned := range busylist[rproc] {
if assigned.Procobjtype == guessed.Procobjtype && assigned.Id == guessed.Id {
present = true
break
}
}
if !present {
resp <- VarAns{ANS_OK, guessed}
// Only in the IO is inittializated its use has to be notified
if rcell.Global_id != 0 {
useditem <- UsageNotify{TR_PROC, rproc, C_OUTPUT, S_NIL, rcell.Global_id}
}
busylist[rproc] = append(busylist[rproc], guessed)
break
}
}
} else {
resp <- VarAns{ANS_FAIL, VarCell{gent, 0, 0, 0, 0, 0, 0, 0}}
}
} else {
panic("Allocator received a wrong type, this cannot happen. A bug is here")
}
case CHANNEL:
if gent, _ := Type_from_string(ri.Config.Basic_chantype); Same_Type(rcell.Vtype, gent) {
if _, ok := busylist[rproc]; !ok {
vcells := make([]VarCell, 0)
busylist[rproc] = vcells
}
guessed_global_id := 0
created := false
for i := 0; i < MAX_CHANNELS; i++ {
guessed_global_id = i
present := false
for _, channel := range busychan {
if channel.Global_id == guessed_global_id {
present = true
break
}
}
if !present {
connected := make([]int, 1)
connected[0] = rproc
readers := make([]int, 0)
writers := make([]int, 0)
busychan = append(busychan, ChanInfo{guessed_global_id, connected, readers, writers})
useditem <- UsageNotify{TR_CHAN, guessed_global_id, C_CONNECTED, S_NIL, rproc}
created = true
break
}
}
if created {
created = false
for i := 0; i < MAX_CHANNELS; i++ {
guessed := VarCell{gent, CHANNEL, i, i, i, guessed_global_id, guessed_global_id, guessed_global_id}
present := false
for _, assigned := range busylist[rproc] {
if assigned.Procobjtype == guessed.Procobjtype && assigned.Id == guessed.Id {
present = true
break
}
}
if !present {
resp <- VarAns{ANS_OK, guessed}
useditem <- UsageNotify{TR_PROC, rproc, C_SHAREDOBJECT, "channel:", I_NIL}
busylist[rproc] = append(busylist[rproc], guessed)
useditem <- UsageNotify{TR_CHAN, guessed_global_id, C_CONNECTED, S_NIL, rproc}
created = true
break
}
}
if !created {
panic("Channel creation failed")
}
} else {
panic("global channel id failed")
}
} else if gent, _ := Type_from_string("chan bool"); Same_Type(rcell.Vtype, gent) {
if _, ok := busylist[rproc]; !ok {
vcells := make([]VarCell, 0)
busylist[rproc] = vcells
}
guessed_global_id := 0
created := false
for i := 0; i < MAX_CHANNELS; i++ {
guessed_global_id = i
present := false
for _, channel := range busychan {
if channel.Global_id == guessed_global_id {
present = true
break
}
}
if !present {
connected := make([]int, 1)
connected[0] = rproc
readers := make([]int, 0)
writers := make([]int, 0)
busychan = append(busychan, ChanInfo{guessed_global_id, connected, readers, writers})
useditem <- UsageNotify{TR_CHAN, guessed_global_id, C_CONNECTED, S_NIL, rproc}
created = true
break
}
}
if created {
created = false
for i := 0; i < MAX_CHANNELS; i++ {
guessed := VarCell{gent, CHANNEL, i, i, i, guessed_global_id, guessed_global_id, guessed_global_id}
present := false
for _, assigned := range busylist[rproc] {
if assigned.Procobjtype == guessed.Procobjtype && assigned.Id == guessed.Id {
present = true
break
}
}
if !present {
resp <- VarAns{ANS_OK, guessed}
useditem <- UsageNotify{TR_PROC, rproc, C_SHAREDOBJECT, "channel:", I_NIL}
busylist[rproc] = append(busylist[rproc], guessed)
useditem <- UsageNotify{TR_CHAN, guessed_global_id, C_CONNECTED, S_NIL, rproc}
created = true
break
}
}
if !created {
panic("Channel creation failed")
}
} else {
panic("global channel id failed")
}
}
}
case REQ_ATTACH:
switch rcell.Procobjtype {
case CHANNEL:
if gent, _ := Type_from_string(ri.Config.Basic_chantype); Same_Type(rcell.Vtype, gent) {
if _, ok := busylist[rproc]; !ok {
vcells := make([]VarCell, 0)
busylist[rproc] = vcells
}
guessed_global_id := rcell.Global_id
created := false
for i := 0; i < MAX_CHANNELS; i++ {
guessed := VarCell{gent, CHANNEL, i, i, i, guessed_global_id, guessed_global_id, guessed_global_id}
present := false
for _, assigned := range busylist[rproc] {
if assigned.Procobjtype == guessed.Procobjtype && assigned.Id == guessed.Id {
present = true
break
}
}
if !present {
resp <- VarAns{ANS_OK, guessed}
useditem <- UsageNotify{TR_PROC, rproc, C_SHAREDOBJECT, "channel:", I_NIL}
busylist[rproc] = append(busylist[rproc], guessed)
busychan[guessed_global_id].Connected = append(busychan[guessed_global_id].Connected, rproc)
useditem <- UsageNotify{TR_CHAN, guessed_global_id, C_CONNECTED, S_NIL, rproc}
created = true
break
}
}
if !created {
panic("channel attach failed")
}
} else if gent, _ := Type_from_string("chan bool"); Same_Type(rcell.Vtype, gent) {
if _, ok := busylist[rproc]; !ok {
vcells := make([]VarCell, 0)
busylist[rproc] = vcells
}
guessed_global_id := rcell.Global_id
created := false
for i := 0; i < MAX_CHANNELS; i++ {
guessed := VarCell{gent, CHANNEL, i, i, i, guessed_global_id, guessed_global_id, guessed_global_id}
present := false
for _, assigned := range busylist[rproc] {
if assigned.Procobjtype == guessed.Procobjtype && assigned.Id == guessed.Id {
present = true
break
}
}
if !present {
resp <- VarAns{ANS_OK, guessed}
useditem <- UsageNotify{TR_PROC, rproc, C_SHAREDOBJECT, "channel:", I_NIL}
busylist[rproc] = append(busylist[rproc], guessed)
busychan[guessed_global_id].Connected = append(busychan[guessed_global_id].Connected, rproc)
useditem <- UsageNotify{TR_CHAN, guessed_global_id, C_CONNECTED, S_NIL, rproc}
created = true
break
}
}
if !created {
panic("channel attach failed")
}
}
}
case REQ_EXIT:
//fmt.Println(busychan)
break VA
}
}
assignerdone <- true
}