summaryrefslogtreecommitdiffstats
path: root/qemu/roms/SLOF/slof/prim.code
blob: 9fbed7168e616774a7a9c7f2972b97b02287eec5 (plain)
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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
/******************************************************************************
 * Copyright (c) 2004, 2008 IBM Corporation
 * All rights reserved.
 * This program and the accompanying materials
 * are made available under the terms of the BSD License
 * which accompanies this distribution, and is available at
 * http://www.opensource.org/licenses/bsd-license.php
 *
 * Contributors:
 *     IBM Corporation - initial implementation
 *****************************************************************************/


//
// Copyright 2002,2003,2004  Segher Boessenkool  <segher@kernel.crashing.org>
//


#define NEXT00	goto *cfa->a
#define NEXT0	cfa = ip->a; NEXT00
#define NEXT	ip++; NEXT0

#define PRIM(name) code_##name: { \
		   asm volatile ("#### " #name : : : "memory"); \
		   void *w = (cfa = (++ip)->a)->a;
#define MIRP	   goto *w; }



	// start interpreting
	NEXT0;



// These macros could be replaced to allow for TOS caching etc.
#define TOS (*dp)
#define NOS (*(dp-1))
#define POP dp--
#define PUSH dp++

#define RTOS (*rp)
#define RNOS (*(rp-1))
#define RPOP rp--
#define RPUSH rp++




// For terminal input.
PRIM(TIB) PUSH; TOS.a = the_tib; MIRP

// For pockets (temporary string buffers).
PRIM(POCKETS) PUSH; TOS.a = the_pockets; MIRP

// exception register area
PRIM(EREGS) PUSH; TOS.a = the_exception_frame; MIRP

// client register area
PRIM(CIREGS) PUSH; TOS.a = the_client_frame; MIRP

// Client stack
//	(According to the PowerPC ABI the stack-pointer points to the
//	 lowest **USED** value.
//	 I.e. it is decremented before a new element is stored on the
//	 stack.)
PRIM(CISTACK) PUSH; TOS.a = the_client_stack
                            + (sizeof(the_client_stack) / CELLSIZE); MIRP

// compile-in-interpret buffer
PRIM(COMP_X2d_BUFFER) PUSH; TOS.a = the_comp_buffer; MIRP

// Paflof base address
PRIM(PAFLOF_X2d_START) PUSH; TOS.a = _start_OF; MIRP

// Heap pointers
PRIM(HEAP_X2d_START) PUSH; TOS.a = the_heap_start; MIRP
PRIM(HEAP_X2d_END) PUSH; TOS.a = the_heap_end; MIRP

// FDT pointer
PRIM(FDT_X2d_START) PUSH; TOS.u = fdt_start; MIRP

// romfs-base
PRIM(ROMFS_X2d_BASE) PUSH; TOS.u = romfs_base; MIRP

// if the low level firmware is epapr compliant it will put the
// epapr magic into r6 before starting paflof
// epapr-magic is a copy of r6
PRIM(EPAPR_X2d_MAGIC) PUSH; TOS.u = epapr_magic; MIRP

// Initially mapped area size (for ePAPR compliant LLFW)
PRIM(EPAPR_X2d_IMA_X2d_SIZE) PUSH; TOS.u = epapr_ima_size; MIRP

// Codefields.
code_DOCOL:
	{
		RPUSH; RTOS.a = ip;
		ip = cfa;
		NEXT;
	}
code_DODOES:
	{
		RPUSH; RTOS.a = ip;
		ip = (cfa + 1)->a;
		PUSH; TOS.a = cfa + 2;
		NEXT0;
	}
code_DODEFER:
	{
		cfa = (cfa + 1)->a;
		NEXT00;
	}
code_DOALIAS:
	{
		cfa = (cfa + 1)->a;
		NEXT00;
	}
code_DOCON:
	{
		PUSH;
		TOS = *(cfa + 1);
		NEXT;
	}
code_DOVAL:
	{
		PUSH;
		TOS = *(cfa + 1);
		NEXT;
	}
code_DOFIELD:
	{
		dp->n += (cfa + 1)->n;
		NEXT;
	}
code_DOVAR:
	{
		(++dp)->a = cfa + 1;
		NEXT;
	}
code_DOBUFFER_X3a:
	{
		(++dp)->a = cfa + 1;
		NEXT;
	}





// branching
code_BRANCH:
	{
		type_n dis = (++ip)->n;
		ip = (cell *)((type_u)ip + dis);
		NEXT;
	}
code_0BRANCH:
	{
		type_n dis = (++ip)->n;
		if (TOS.u == 0)
			ip = (cell *)((type_u)ip + dis);
		POP;
		NEXT;
	}

// Jump to "defer BP"
code_BREAKPOINT:
	{
		RPUSH; RTOS.a = ip;
		ip = (cell * ) xt_BP+2;
		NEXT;
	}


// literals
code_LIT:
	{
		PUSH;
		TOS = *++ip;
		NEXT;
	}
code_DOTICK:
	{
		PUSH;
		TOS = *++ip;
		NEXT;
	}



// 1.1
PRIM(DUP) cell x = TOS; PUSH; TOS = x; MIRP
PRIM(OVER) cell x = NOS; PUSH; TOS = x; MIRP
PRIM(PICK) TOS = *(dp - TOS.n - 1); MIRP

// 1.2
PRIM(DROP) POP; MIRP

// 1.3
PRIM(SWAP) cell x = NOS; NOS = TOS; TOS = x; MIRP

// 1.4
PRIM(_X3e_R) RPUSH; RTOS = TOS; POP; MIRP
PRIM(R_X3e) PUSH; TOS = RTOS; RPOP; MIRP
PRIM(R_X40) PUSH; TOS = RTOS; MIRP

// 1.5
PRIM(DEPTH) PUSH; TOS.u = dp - the_data_stack; MIRP
PRIM(DEPTH_X21) dp = the_data_stack + TOS.u - 1; MIRP
PRIM(RDEPTH) PUSH; TOS.u = rp - the_return_stack + 1; MIRP
PRIM(RDEPTH_X21) rp = the_return_stack + TOS.u - 1; POP; MIRP
PRIM(RPICK) TOS = *(rp - TOS.n); MIRP

// 2.1
PRIM(_X2b) NOS.u += TOS.u; POP; MIRP
PRIM(_X2d) NOS.u -= TOS.u; POP; MIRP
PRIM(_X2a) NOS.u *= TOS.u; POP; MIRP

// 2.2
PRIM(LSHIFT) NOS.u <<= TOS.u; POP; MIRP
PRIM(RSHIFT) NOS.u >>= TOS.u; POP; MIRP
PRIM(ASHIFT) NOS.n >>= TOS.u; POP; MIRP
PRIM(AND) NOS.u &= TOS.u; POP; MIRP
PRIM(OR) NOS.u |= TOS.u; POP; MIRP
PRIM(XOR) NOS.u ^= TOS.u; POP; MIRP

// 3.1
#define GET_TYPE1(t) { \
		t *restrict a = (t *restrict)(TOS.a); \
		t b;

#define GET_TYPE2(t) \
		b = *a;

#define GET_TYPE3(t) \
		TOS.u = b; \
}

#define PUT_TYPE1(t) { \
		t *restrict a = TOS.a; \
		t b = NOS.u; \
		POP; \
		POP;

#define PUT_TYPE2(t) \
		*a = b; \
}

#define GET_CELL1 GET_TYPE1(type_u)
#define PUT_CELL1 PUT_TYPE1(type_u)
#define GET_CHAR1 GET_TYPE1(type_c)
#define PUT_CHAR1 PUT_TYPE1(type_c)
#define GET_WORD1 GET_TYPE1(type_w)
#define PUT_WORD1 PUT_TYPE1(type_w)
#define GET_LONG1 GET_TYPE1(type_l)
#define PUT_LONG1 PUT_TYPE1(type_l)
#define GET_XONG1 GET_TYPE1(type_u)
#define PUT_XONG1 PUT_TYPE1(type_u)

#define GET_CELL2 GET_TYPE2(type_u)
#define PUT_CELL2 PUT_TYPE2(type_u)
#define GET_CHAR2 GET_TYPE2(type_c)
#define PUT_CHAR2 PUT_TYPE2(type_c)
#define GET_WORD2 GET_TYPE2(type_w)
#define PUT_WORD2 PUT_TYPE2(type_w)
#define GET_LONG2 GET_TYPE2(type_l)
#define PUT_LONG2 PUT_TYPE2(type_l)
#define GET_XONG2 GET_TYPE2(type_u)
#define PUT_XONG2 PUT_TYPE2(type_u)

#define GET_CELL3 GET_TYPE3(type_u)
#define GET_CHAR3 GET_TYPE3(type_c)
#define GET_WORD3 GET_TYPE3(type_w)
#define GET_LONG3 GET_TYPE3(type_l)
#define GET_XONG3 GET_TYPE3(type_u)

#define GET_CELL GET_CELL1 GET_CELL2 GET_CELL3
#define PUT_CELL PUT_CELL1 PUT_CELL2
#define GET_CHAR GET_CHAR1 GET_CHAR2 GET_CHAR3
#define PUT_CHAR PUT_CHAR1 PUT_CHAR2
#define GET_WORD GET_WORD1 GET_WORD2 GET_WORD3
#define PUT_WORD PUT_WORD1 PUT_WORD2
#define GET_LONG GET_LONG1 GET_LONG2 GET_LONG3
#define PUT_LONG PUT_LONG1 PUT_LONG2
#define GET_XONG GET_XONG1 GET_XONG2 GET_XONG3
#define PUT_XONG PUT_XONG1 PUT_XONG2

	PRIM(_X40)  GET_CELL; MIRP
	PRIM(_X21)  PUT_CELL; MIRP
	PRIM(C_X40) GET_CHAR; MIRP
	PRIM(C_X21) PUT_CHAR; MIRP
	PRIM(W_X40) GET_WORD; MIRP
	PRIM(W_X21) PUT_WORD; MIRP
	PRIM(L_X40) GET_LONG; MIRP
	PRIM(L_X21) PUT_LONG; MIRP
	PRIM(X_X40) GET_XONG; MIRP
	PRIM(X_X21) PUT_XONG; MIRP


#define UGET_TYPE1(t) { \
		type_c *restrict a = (type_c *restrict)(TOS.a); \
		t b; \
		type_c *restrict c = (type_c *restrict)&b;

#define UGET_TYPE2(t) \
		*c++ = *a++; \
		*c++ = *a++;

#define UGET_TYPE3(t) \
		TOS.u = b; \
}

#define UPUT_TYPE1(t) { \
		type_c *restrict a = (type_c *restrict)(TOS.a); \
		t b = NOS.u; \
		type_c *restrict c = (type_c *restrict)&b; \
		POP; \
		POP;

#define UPUT_TYPE2(t) \
		*a++ = *c++; \
		*a++ = *c++;

#define UPUT_TYPE3(t) }

#define UGET_WORD1 UGET_TYPE1(type_w)
#define UPUT_WORD1 UPUT_TYPE1(type_w)
#define UGET_WORD2 UGET_TYPE2(type_w)
#define UPUT_WORD2 UPUT_TYPE2(type_w)
#define UGET_WORD3 UGET_TYPE3(type_w)
#define UPUT_WORD3 UPUT_TYPE3(type_w)
#define UGET_LONG1 UGET_TYPE1(type_l)
#define UPUT_LONG1 UPUT_TYPE1(type_l)
#define UGET_LONG2 UGET_TYPE2(type_l)
#define UPUT_LONG2 UPUT_TYPE2(type_l)
#define UGET_LONG3 UGET_TYPE3(type_l)
#define UPUT_LONG3 UPUT_TYPE3(type_l)

#define UGET_WORD UGET_WORD1 UGET_WORD2 UGET_WORD3
#define UPUT_WORD UPUT_WORD1 UPUT_WORD2 UPUT_WORD3
#define UGET_LONG UGET_LONG1 UGET_LONG2 UGET_LONG2 UGET_LONG3
#define UPUT_LONG UPUT_LONG1 UPUT_LONG2 UPUT_LONG2 UPUT_LONG3

	PRIM(UNALIGNED_X2d_W_X40) UGET_WORD; MIRP
	PRIM(UNALIGNED_X2d_W_X21) UPUT_WORD; MIRP
	PRIM(UNALIGNED_X2d_L_X40) UGET_LONG; MIRP
	PRIM(UNALIGNED_X2d_L_X21) UPUT_LONG; MIRP


// 6
PRIM(_X3c) NOS.n = -(NOS.n < TOS.n); POP; MIRP
PRIM(U_X3c) NOS.n = -(NOS.u < TOS.u); POP; MIRP
PRIM(0_X3c) TOS.n = -(TOS.n < 0); MIRP
PRIM(_X3d) NOS.n = -(NOS.u == TOS.u); POP; MIRP
PRIM(0_X3d) TOS.n = -(TOS.u == 0); MIRP






// 8.4
PRIM(DODO) RPUSH; RTOS = NOS; RPUSH; RTOS = TOS; POP; POP; MIRP
code_DO_X3f_DO:
	{
		cell i = *dp--;
		cell n = *dp--;
		type_n dis = (++ip)->n;
		if (i.n == n.n)
			ip = (cell *restrict)((type_c *restrict)ip + dis);
		else {
			*(rp + 1) = n;
			*(rp += 2) = i;
		}
		NEXT;
	}
code_DOLOOP:
	{
		type_n dis = (++ip)->n;
		rp->n++;
		if (rp->n == (rp - 1)->n)
			rp -= 2;
		else
			ip = (cell *restrict)((type_c *restrict)ip + dis);
		NEXT;
	}
code_DO_X2b_LOOP:
	{
		type_u lo, hi;
		type_n inc;
		type_n dis = (++ip)->n;
		lo = rp->u;
		inc = (dp--)->n;
		rp->n += inc;
		if (inc >= 0)
			hi = rp->u;
		else {
			hi = lo;
			lo = rp->u;
		}
		if ((type_u)((rp - 1)->n - 1 - lo) < hi - lo)
			rp -= 2;
		else
			ip = (cell *restrict)((type_c *restrict)ip + dis);
		NEXT;
	}
code_DOLEAVE:
	{
		type_n dis = (++ip)->n;
		rp -= 2;
		ip = (cell *restrict)((type_c *restrict)ip + dis);
		NEXT;
	}
code_DO_X3f_LEAVE:
	{
		type_n dis = (++ip)->n;
		if ((dp--)->n) {
			rp -= 2;
			ip = (cell *restrict)((type_c *restrict)ip + dis);
		}
		NEXT;
	}






// 8.5
code_EXIT:
	{
		ip = (rp--)->a;
		NEXT;
	}

code_SEMICOLON:
	{
		ip = (rp--)->a;
		NEXT;
	}

code_EXECUTE:	// don't need this as prim
	{
		cfa = (dp--)->a;
		NEXT00;
	}


PRIM(MOVE)
	type_u n = TOS.u; POP;
	unsigned char *q = TOS.a; POP;
	unsigned char *p = TOS.a; POP;

	_FASTMOVE(p, q, n);
MIRP

code_FILL:
	{
		unsigned char c = (dp--)->u;
		type_n size = ((dp--)->n);
		unsigned char *d = (unsigned char *)((dp--)->u);
		type_u fill_v=c | c <<8;

		fill_v |= fill_v << 16;
		switch (((type_u)d | (type_u)size) & (sizeof(type_u)-1)) {
			case 0: {
				type_u *up = (type_u *)d;
#if (__LONG_MAX__ > 2147483647L)
				fill_v |= fill_v << 32;
#endif
				while ((size-=sizeof(type_u)) >= 0)
					*up++ = fill_v;
			}
			case sizeof(type_l): {
				type_l *lp = (type_l *)d;

				while ((size-=sizeof(type_l)) >= 0)
					*lp++ = (type_l)fill_v;
			}
			case sizeof(type_w): {
				type_w *wp = (type_w *)d;

				while ((size-=sizeof(type_w)) >= 0)
					*wp++ = (type_w)fill_v;
			}
			default:
				while (size-- > 0)
					*d++ = (unsigned char)c;
		}
		NEXT;
	}

code_COMP:
	{
		type_n len = ((dp--)->n);
		unsigned char *addr2 = (unsigned char *)((dp--)->u);
		unsigned char *addr1 = (unsigned char *)((dp--)->u);

		while (len-- > 0) {
			if (*addr1 > *addr2) {
				(++dp)->n = 1;
				NEXT;
			}
			else if (*addr1 < *addr2) {
				(++dp)->n = -1;
				NEXT;
			}
			addr1 += 1;
			addr2 += 1;
		}
		(++dp)->n = 0;
		NEXT;
	}


PRIM(RMOVE)
	type_u size = ((dp--)->u);
	type_u *d = (type_u *)((dp--)->u);
	type_u *s = (type_u *)((dp--)->u);
	_FASTRMOVE(s, d, size);

	MIRP


// String compare, case insensitive:
// : string=ci  ( str1 len1 str2 len2 -- equal? )
PRIM(STRING_X3d_CI)
	type_u l2 = TOS.u; POP;
	unsigned char *p2 = TOS.a; POP;
	type_u l1 = TOS.u; POP;
	unsigned char *p1 = TOS.a;

	if (l1 == l2) {
		TOS.n = -1;		/* Default to TRUE */
		while (l1 > 0) {
			if (toupper(*p1) != toupper(*p2)) {
				TOS.n = 0;
				break;
			}
			++p1;  ++p2;
			--l1;
		}
	}
	else {
		TOS.n = 0;
	}
MIRP

// bool dependend pick
// ?PICK ( v1 v2 bool -- v1|v2 )
PRIM(_X3f_PICK)
        type_u b = TOS.u; POP;
        if (b) { NOS = TOS; }
        POP;
MIRP

/* zcount ( zstr -- str len ) */
PRIM(ZCOUNT)
	type_u len = strlen(TOS.a);
	PUSH; TOS.u = len;
MIRP

PRIM(CLEAN_X2d_HASH)
	memset(hash_table, 0, sizeof(hash_table));
MIRP

PRIM(HASH_X2d_TABLE)
	PUSH;
	TOS.a = hash_table;
MIRP

/* hash ( str len -- hash )
 * this word is used in find-hash.fs to create
 * a hash to accelerate word lookup */
PRIM(HASH)
	type_u len = TOS.u; POP;
	unsigned char *str = TOS.a;
	type_u tmp = len;
	type_u hash = 0;
	while(len--) {
		hash <<= 1;
		hash ^= tolower(*str);
		hash ^= tmp;
		str++;
	}
	/* we only want hash values which size is smaller
	 * than HASHSIZE */
	hash &= HASHSIZE - 1;
	/* access the hash table in steps of CELLSIZE */
	hash *= CELLSIZE;
	/* return a pointer for this hash in the hash table */
	TOS.a = hash_table + hash;
MIRP