Debian Codegraph @

src/inc/opcodes.h

raw
1 // SPDX-License-Identifier: MIT OR MPL-2.0 OR LGPL-2.1-or-later OR GPL-2.0-or-later
2 // Copyright 2010, SIL International, All rights reserved.
3
4 #pragma once
5 // This file will be pulled into and integrated into a machine implmentation
6 // DO NOT build directly and under no circumstances ever #include headers in
7 // here or you will break the direct_machine.
8 //
9 // Implementers' notes
10 // ==================
11 // You have access to a few primitives and the full C++ code:
12 // declare_params(n) Tells the interpreter how many bytes of parameter
13 // space to claim for this instruction uses and
14 // initialises the param pointer. You *must* before the
15 // first use of param.
16 // use_params(n) Claim n extra bytes of param space beyond what was
17 // claimed using delcare_param.
18 // param A const byte pointer for the parameter space claimed by
19 // this instruction.
20 // binop(op) Implement a binary operation on the stack using the
21 // specified C++ operator.
22 // NOT_IMPLEMENTED Any instruction body containing this will exit the
23 // program with an assertion error. Instructions that are
24 // not implemented should also be marked NILOP in the
25 // opcodes tables this will cause the code class to spot
26 // them in a live code stream and throw a runtime_error
27 // instead.
28 // push(n) Push the value n onto the stack.
29 // pop() Pop the top most value and return it.
30 //
31 // You have access to the following named fast 'registers':
32 // sp = The pointer to the current top of stack, the last value
33 // pushed.
34 // seg = A reference to the Segment this code is running over.
35 // is = The current slot index
36 // isb = The original base slot index at the start of this rule
37 // isf = The first positioned slot
38 // isl = The last positioned slot
39 // ip = The current instruction pointer
40 // endPos = Position of advance of last cluster
41 // dir = writing system directionality of the font
42
43
44 // #define NOT_IMPLEMENTED assert(false)
45 // #define NOT_IMPLEMENTED
46
47 #define binop(op) const uint32 a = pop(); *sp = uint32(*sp) op a
48 #define sbinop(op) const int32 a = pop(); *sp = int32(*sp) op a
49 #define use_params(n) dp += n
50
51 #define declare_params(n) const byte * param = dp; \
52 use_params(n);
53
54 #define push(n) { *++sp = n; }
55 #define pop() (*sp--)
56 #define slotat(x) ((map + (x) >= &smap[-1] && map + (x) < smap.end()) ? \
57 map[(x)] : (status = Machine::slot_offset_out_bounds, nullptr))
58 #define DIE { is=seg.last(); status = Machine::died_early; EXIT(1); }
59 #define POSITIONED 1
60
61 STARTOP(nop)
62 do {} while (0);
63 ENDOP
64
65 STARTOP(push_byte)
66 declare_params(1);
67 push(int8(*param));
68 ENDOP
69
70 STARTOP(push_byte_u)
71 declare_params(1);
72 push(uint8(*param));
73 ENDOP
74
75 STARTOP(push_short)
76 declare_params(2);
77 const int16 r = int16(param[0]) << 8
78 | uint8(param[1]);
79 push(r);
80 ENDOP
81
82 STARTOP(push_short_u)
83 declare_params(2);
84 const uint16 r = uint16(param[0]) << 8
85 | uint8(param[1]);
86 push(r);
87 ENDOP
88
89 STARTOP(push_long)
90 declare_params(4);
91 const int32 r = int32(param[0]) << 24
92 | uint32(param[1]) << 16
93 | uint32(param[2]) << 8
94 | uint8(param[3]);
95 push(r);
96 ENDOP
97
98 STARTOP(add)
99 binop(+);
100 ENDOP
101
102 STARTOP(sub)
103 binop(-);
104 ENDOP
105
106 STARTOP(mul)
107 binop(*);
108 ENDOP
109
110 STARTOP(div_)
111 const int32 b = pop();
112 const int32 a = int32(*sp);
113 if (b == 0 || (a == std::numeric_limits<int32>::min() && b == -1)) DIE;
114 *sp = int32(*sp) / b;
115 ENDOP
116
117 STARTOP(min_)
118 const int32 a = pop(), b = *sp;
119 if (a < b) *sp = a;
120 ENDOP
121
122 STARTOP(max_)
123 const int32 a = pop(), b = *sp;
124 if (a > b) *sp = a;
125 ENDOP
126
127 STARTOP(neg)
128 *sp = uint32(-int32(*sp));
129 ENDOP
130
131 STARTOP(trunc8)
132 *sp = uint8(*sp);
133 ENDOP
134
135 STARTOP(trunc16)
136 *sp = uint16(*sp);
137 ENDOP
138
139 STARTOP(cond)
140 const uint32 f = pop(), t = pop(), c = pop();
141 push(c ? t : f);
142 ENDOP
143
144 STARTOP(and_)
145 binop(&&);
146 ENDOP
147
148 STARTOP(or_)
149 binop(||);
150 ENDOP
151
152 STARTOP(not_)
153 *sp = !*sp;
154 ENDOP
155
156 STARTOP(equal)
157 binop(==);
158 ENDOP
159
160 STARTOP(not_eq_)
161 binop(!=);
162 ENDOP
163
164 STARTOP(less)
165 sbinop(<);
166 ENDOP
167
168 STARTOP(gtr)
169 sbinop(>);
170 ENDOP
171
172 STARTOP(less_eq)
173 sbinop(<=);
174 ENDOP
175
176 STARTOP(gtr_eq)
177 sbinop(>=);
178 ENDOP
179
180 STARTOP(next)
181 if (map - &smap[0] >= int(smap.size())) DIE
182 if (is)
183 {
184 if (is == smap.highwater())
185 smap.highpassed(true);
186 is = is->next();
187 }
188 ++map;
189 ENDOP
190
191 //STARTOP(next_n)
192 // use_params(1);
193 // NOT_IMPLEMENTED;
194 //declare_params(1);
195 //const size_t num = uint8(*param);
196 //ENDOP
197
198 //STARTOP(copy_next)
199 // if (is) is = is->next();
200 // ++map;
201 // ENDOP
202
203 STARTOP(put_glyph_8bit_obs)
204 declare_params(1);
205 const unsigned int output_class = uint8(*param);
206 is->setGlyph(&seg, seg.getClassGlyph(output_class, 0));
207 ENDOP
208
209 STARTOP(put_subs_8bit_obs)
210 declare_params(3);
211 const int slot_ref = int8(param[0]);
212 const unsigned int input_class = uint8(param[1]),
213 output_class = uint8(param[2]);
214 uint16 index;
215 slotref slot = slotat(slot_ref);
216 if (slot)
217 {
218 index = seg.findClassIndex(input_class, slot->gid());
219 is->setGlyph(&seg, seg.getClassGlyph(output_class, index));
220 }
221 ENDOP
222
223 STARTOP(put_copy)
224 declare_params(1);
225 const int slot_ref = int8(*param);
226 if (is && !is->isDeleted())
227 {
228 slotref ref = slotat(slot_ref);
229 if (ref && ref != is)
230 {
231 int16 *tempUserAttrs = is->userAttrs();
232 if (is->attachedTo() || is->firstChild()) DIE
233 Slot *prev = is->prev();
234 Slot *next = is->next();
235 memcpy(tempUserAttrs, ref->userAttrs(), seg.numAttrs() * sizeof(uint16));
236 memcpy(is, ref, sizeof(Slot));
237 is->firstChild(NULL);
238 is->nextSibling(NULL);
239 is->userAttrs(tempUserAttrs);
240 is->next(next);
241 is->prev(prev);
242 if (is->attachedTo())
243 is->attachedTo()->child(is);
244 }
245 is->markCopied(false);
246 is->markDeleted(false);
247 }
248 ENDOP
249
250 STARTOP(insert)
251 if (smap.decMax() <= 0) DIE;
252 Slot *newSlot = seg.newSlot();
253 if (!newSlot) DIE;
254 Slot *iss = is;
255 while (iss && iss->isDeleted()) iss = iss->next();
256 if (!iss)
257 {
258 if (seg.last())
259 {
260 seg.last()->next(newSlot);
261 newSlot->prev(seg.last());
262 newSlot->before(seg.last()->before());
263 seg.last(newSlot);
264 }
265 else
266 {
267 seg.first(newSlot);
268 seg.last(newSlot);
269 }
270 }
271 else if (iss->prev())
272 {
273 iss->prev()->next(newSlot);
274 newSlot->prev(iss->prev());
275 newSlot->before(iss->prev()->after());
276 }
277 else
278 {
279 newSlot->prev(NULL);
280 newSlot->before(iss->before());
281 seg.first(newSlot);
282 }
283 newSlot->next(iss);
284 if (iss)
285 {
286 iss->prev(newSlot);
287 newSlot->originate(iss->original());
288 newSlot->after(iss->before());
289 }
290 else if (newSlot->prev())
291 {
292 newSlot->originate(newSlot->prev()->original());
293 newSlot->after(newSlot->prev()->after());
294 }
295 else
296 {
297 newSlot->originate(seg.defaultOriginal());
298 }
299 if (is == smap.highwater())
300 smap.highpassed(false);
301 is = newSlot;
302 seg.extendLength(1);
303 if (map != &smap[-1])
304 --map;
305 ENDOP
306
307 STARTOP(delete_)
308 if (!is || is->isDeleted()) DIE
309 is->markDeleted(true);
310 if (is->prev())
311 is->prev()->next(is->next());
312 else
313 seg.first(is->next());
314
315 if (is->next())
316 is->next()->prev(is->prev());
317 else
318 seg.last(is->prev());
319
320
321 if (is == smap.highwater())
322 smap.highwater(is->next());
323 if (is->prev())
324 is = is->prev();
325 seg.extendLength(-1);
326 ENDOP
327
328 STARTOP(assoc)
329 declare_params(1);
330 unsigned int num = uint8(*param);
331 const int8 * assocs = reinterpret_cast<const int8 *>(param+1);
332 use_params(num);
333 int max = -1;
334 int min = -1;
335
336 while (num-- > 0)
337 {
338 int sr = *assocs++;
339 slotref ts = slotat(sr);
340 if (ts && (min == -1 || ts->before() < min)) min = ts->before();
341 if (ts && ts->after() > max) max = ts->after();
342 }
343 if (min > -1) // implies max > -1
344 {
345 is->before(min);
346 is->after(max);
347 }
348 ENDOP
349
350 STARTOP(cntxt_item)
351 // It turns out this is a cunningly disguised condition forward jump.
352 declare_params(3);
353 const int is_arg = int8(param[0]);
354 const size_t iskip = uint8(param[1]),
355 dskip = uint8(param[2]);
356
357 if (mapb + is_arg != map)
358 {
359 ip += iskip;
360 dp += dskip;
361 push(true);
362 }
363 ENDOP
364
365 STARTOP(attr_set)
366 declare_params(1);
367 const attrCode slat = attrCode(uint8(*param));
368 const int val = pop();
369 is->setAttr(&seg, slat, 0, val, smap);
370 ENDOP
371
372 STARTOP(attr_add)
373 declare_params(1);
374 const attrCode slat = attrCode(uint8(*param));
375 const uint32_t val = pop();
376 if ((slat == gr_slatPosX || slat == gr_slatPosY) && (flags & POSITIONED) == 0)
377 {
378 seg.positionSlots(0, *smap.begin(), *(smap.end()-1), seg.currdir());
379 flags |= POSITIONED;
380 }
381 uint32_t res = uint32_t(is->getAttr(&seg, slat, 0));
382 is->setAttr(&seg, slat, 0, int32_t(val + res), smap);
383 ENDOP
384
385 STARTOP(attr_sub)
386 declare_params(1);
387 const attrCode slat = attrCode(uint8(*param));
388 const uint32_t val = pop();
389 if ((slat == gr_slatPosX || slat == gr_slatPosY) && (flags & POSITIONED) == 0)
390 {
391 seg.positionSlots(0, *smap.begin(), *(smap.end()-1), seg.currdir());
392 flags |= POSITIONED;
393 }
394 uint32_t res = uint32_t(is->getAttr(&seg, slat, 0));
395 is->setAttr(&seg, slat, 0, int32_t(res - val), smap);
396 ENDOP
397
398 STARTOP(attr_set_slot)
399 declare_params(1);
400 const attrCode slat = attrCode(uint8(*param));
401 const int offset = int(map - smap.begin())*int(slat == gr_slatAttTo);
402 const int val = pop() + offset;
403 is->setAttr(&seg, slat, offset, val, smap);
404 ENDOP
405
406 STARTOP(iattr_set_slot)
407 declare_params(2);
408 const attrCode slat = attrCode(uint8(param[0]));
409 const uint8 idx = uint8(param[1]);
410 const int val = int(pop() + (map - smap.begin())*int(slat == gr_slatAttTo));
411 is->setAttr(&seg, slat, idx, val, smap);
412 ENDOP
413
414 STARTOP(push_slot_attr)
415 declare_params(2);
416 const attrCode slat = attrCode(uint8(param[0]));
417 const int slot_ref = int8(param[1]);
418 if ((slat == gr_slatPosX || slat == gr_slatPosY) && (flags & POSITIONED) == 0)
419 {
420 seg.positionSlots(0, *smap.begin(), *(smap.end()-1), seg.currdir());
421 flags |= POSITIONED;
422 }
423 slotref slot = slotat(slot_ref);
424 if (slot)
425 {
426 int res = slot->getAttr(&seg, slat, 0);
427 push(res);
428 }
429 ENDOP
430
431 STARTOP(push_glyph_attr_obs)
432 declare_params(2);
433 const unsigned int glyph_attr = uint8(param[0]);
434 const int slot_ref = int8(param[1]);
435 slotref slot = slotat(slot_ref);
436 if (slot)
437 push(int32(seg.glyphAttr(slot->gid(), glyph_attr)));
438 ENDOP
439
440 STARTOP(push_glyph_metric)
441 declare_params(3);
442 const unsigned int glyph_attr = uint8(param[0]);
443 const int slot_ref = int8(param[1]);
444 const signed int attr_level = uint8(param[2]);
445 slotref slot = slotat(slot_ref);
446 if (slot)
447 push(seg.getGlyphMetric(slot, glyph_attr, attr_level, dir));
448 ENDOP
449
450 STARTOP(push_feat)
451 declare_params(2);
452 const unsigned int feat = uint8(param[0]);
453 const int slot_ref = int8(param[1]);
454 slotref slot = slotat(slot_ref);
455 if (slot)
456 {
457 uint8 fid = seg.charinfo(slot->original())->fid();
458 push(seg.getFeature(fid, feat));
459 }
460 ENDOP
461
462 STARTOP(push_att_to_gattr_obs)
463 declare_params(2);
464 const unsigned int glyph_attr = uint8(param[0]);
465 const int slot_ref = int8(param[1]);
466 slotref slot = slotat(slot_ref);
467 if (slot)
468 {
469 slotref att = slot->attachedTo();
470 if (att) slot = att;
471 push(int32(seg.glyphAttr(slot->gid(), glyph_attr)));
472 }
473 ENDOP
474
475 STARTOP(push_att_to_glyph_metric)
476 declare_params(3);
477 const unsigned int glyph_attr = uint8(param[0]);
478 const int slot_ref = int8(param[1]);
479 const signed int attr_level = uint8(param[2]);
480 slotref slot = slotat(slot_ref);
481 if (slot)
482 {
483 slotref att = slot->attachedTo();
484 if (att) slot = att;
485 push(int32(seg.getGlyphMetric(slot, glyph_attr, attr_level, dir)));
486 }
487 ENDOP
488
489 STARTOP(push_islot_attr)
490 declare_params(3);
491 const attrCode slat = attrCode(uint8(param[0]));
492 const int slot_ref = int8(param[1]),
493 idx = uint8(param[2]);
494 if ((slat == gr_slatPosX || slat == gr_slatPosY) && (flags & POSITIONED) == 0)
495 {
496 seg.positionSlots(0, *smap.begin(), *(smap.end()-1), seg.currdir());
497 flags |= POSITIONED;
498 }
499 slotref slot = slotat(slot_ref);
500 if (slot)
501 {
502 int res = slot->getAttr(&seg, slat, idx);
503 push(res);
504 }
505 ENDOP
506
507 #if 0
508 STARTOP(push_iglyph_attr) // not implemented
509 NOT_IMPLEMENTED;
510 ENDOP
511 #endif
512
513 STARTOP(pop_ret)
514 const uint32 ret = pop();
515 EXIT(ret);
516 ENDOP
517
518 STARTOP(ret_zero)
519 EXIT(0);
520 ENDOP
521
522 STARTOP(ret_true)
523 EXIT(1);
524 ENDOP
525
526 STARTOP(iattr_set)
527 declare_params(2);
528 const attrCode slat = attrCode(uint8(param[0]));
529 const uint8 idx = uint8(param[1]);
530 const int val = pop();
531 is->setAttr(&seg, slat, idx, val, smap);
532 ENDOP
533
534 STARTOP(iattr_add)
535 declare_params(2);
536 const attrCode slat = attrCode(uint8(param[0]));
537 const uint8 idx = uint8(param[1]);
538 const uint32_t val = pop();
539 if ((slat == gr_slatPosX || slat == gr_slatPosY) && (flags & POSITIONED) == 0)
540 {
541 seg.positionSlots(0, *smap.begin(), *(smap.end()-1), seg.currdir());
542 flags |= POSITIONED;
543 }
544 uint32_t res = uint32_t(is->getAttr(&seg, slat, idx));
545 is->setAttr(&seg, slat, idx, int32_t(val + res), smap);
546 ENDOP
547
548 STARTOP(iattr_sub)
549 declare_params(2);
550 const attrCode slat = attrCode(uint8(param[0]));
551 const uint8 idx = uint8(param[1]);
552 const uint32_t val = pop();
553 if ((slat == gr_slatPosX || slat == gr_slatPosY) && (flags & POSITIONED) == 0)
554 {
555 seg.positionSlots(0, *smap.begin(), *(smap.end()-1), seg.currdir());
556 flags |= POSITIONED;
557 }
558 uint32_t res = uint32_t(is->getAttr(&seg, slat, idx));
559 is->setAttr(&seg, slat, idx, int32_t(res - val), smap);
560 ENDOP
561
562 STARTOP(push_proc_state)
563 use_params(1);
564 push(1);
565 ENDOP
566
567 STARTOP(push_version)
568 push(0x00030000);
569 ENDOP
570
571 STARTOP(put_subs)
572 declare_params(5);
573 const int slot_ref = int8(param[0]);
574 const unsigned int input_class = uint8(param[1]) << 8
575 | uint8(param[2]);
576 const unsigned int output_class = uint8(param[3]) << 8
577 | uint8(param[4]);
578 slotref slot = slotat(slot_ref);
579 if (slot)
580 {
581 int index = seg.findClassIndex(input_class, slot->gid());
582 is->setGlyph(&seg, seg.getClassGlyph(output_class, index));
583 }
584 ENDOP
585
586 #if 0
587 STARTOP(put_subs2) // not implemented
588 NOT_IMPLEMENTED;
589 ENDOP
590
591 STARTOP(put_subs3) // not implemented
592 NOT_IMPLEMENTED;
593 ENDOP
594 #endif
595
596 STARTOP(put_glyph)
597 declare_params(2);
598 const unsigned int output_class = uint8(param[0]) << 8
599 | uint8(param[1]);
600 is->setGlyph(&seg, seg.getClassGlyph(output_class, 0));
601 ENDOP
602
603 STARTOP(push_glyph_attr)
604 declare_params(3);
605 const unsigned int glyph_attr = uint8(param[0]) << 8
606 | uint8(param[1]);
607 const int slot_ref = int8(param[2]);
608 slotref slot = slotat(slot_ref);
609 if (slot)
610 push(int32(seg.glyphAttr(slot->gid(), glyph_attr)));
611 ENDOP
612
613 STARTOP(push_att_to_glyph_attr)
614 declare_params(3);
615 const unsigned int glyph_attr = uint8(param[0]) << 8
616 | uint8(param[1]);
617 const int slot_ref = int8(param[2]);
618 slotref slot = slotat(slot_ref);
619 if (slot)
620 {
621 slotref att = slot->attachedTo();
622 if (att) slot = att;
623 push(int32(seg.glyphAttr(slot->gid(), glyph_attr)));
624 }
625 ENDOP
626
627 STARTOP(temp_copy)
628 slotref newSlot = seg.newSlot();
629 if (!newSlot || !is) DIE;
630 int16 *tempUserAttrs = newSlot->userAttrs();
631 memcpy(newSlot, is, sizeof(Slot));
632 memcpy(tempUserAttrs, is->userAttrs(), seg.numAttrs() * sizeof(uint16));
633 newSlot->userAttrs(tempUserAttrs);
634 newSlot->markCopied(true);
635 *map = newSlot;
636 ENDOP
637
638 STARTOP(band)
639 binop(&);
640 ENDOP
641
642 STARTOP(bor)
643 binop(|);
644 ENDOP
645
646 STARTOP(bnot)
647 *sp = ~*sp;
648 ENDOP
649
650 STARTOP(setbits)
651 declare_params(4);
652 const uint16 m = uint16(param[0]) << 8
653 | uint8(param[1]);
654 const uint16 v = uint16(param[2]) << 8
655 | uint8(param[3]);
656 *sp = ((*sp) & ~m) | v;
657 ENDOP
658
659 STARTOP(set_feat)
660 declare_params(2);
661 const unsigned int feat = uint8(param[0]);
662 const int slot_ref = int8(param[1]);
663 slotref slot = slotat(slot_ref);
664 if (slot)
665 {
666 uint8 fid = seg.charinfo(slot->original())->fid();
667 seg.setFeature(fid, feat, pop());
668 }
669 ENDOP