BAM
Abstract Machine for Bottom-Up Evaluation with the Push Method
bam.h
Go to the documentation of this file.
1 // ============================================================================
2 // Project: Deductive Database
3 // Filename: bam.h
4 // Purpose: Interpreter for Bottom-Up Abstract Machine
5 // Last Change: 19.09.2017
6 // Language: C++
7 // EMail: brass@informatik.uni-halle.de
8 // WWW: http://www.informatik.uni-halle.de/~brass/
9 // Address: Feldschloesschen 15, D-06120 Halle (Saale), GERMANY
10 // Copyright: (c) 2017 by Stefan Brass
11 // License: See file "LICENSE" for copying conditions.
12 // Note: There is no warranty at all - this code may contain bugs.
13 // ============================================================================
14 
15 
27 //=============================================================================
28 // Include File Frame:
29 //=============================================================================
30 
31 #ifndef BAM_INCLUDED
32 #define BAM_INCLUDED
33 
34 //=============================================================================
35 // Used Types and Macros:
36 //=============================================================================
37 
38 #ifndef VER_INCLUDED
39 #include "../base/ver.h"
40 #endif
41 
42 #ifndef STR_INCLUDED
43 #include "../base/str.h"
44 #endif
45 
46 #ifndef CHECK_INCLUDED
47 #include "../base/check.h"
48 #endif
49 
50 #ifndef MPOOL_INCLUDED
51 #include "../mem/mpool.h"
52 #endif
53 
54 #ifndef FLEXARR_INCLUDED
55 #include "../bds/flexarr.h"
56 #endif
57 
58 #ifndef STACK_INCLUDED
59 #include "../bds/stack.h"
60 #endif
61 
62 #ifndef STRMEM_INCLUDED
63 #include "../dom/strmem.h"
64 #endif
65 
66 #ifndef LIST_2_INCLUDED
67 #include "../rel/list_2.h"
68 #endif
69 
70 #ifndef CUR_2_INCLUDED
71 #include "../rel/cur_2.h"
72 #endif
73 
74 #ifndef SET_2_INCLUDED
75 #include "../rel/set_2.h"
76 #endif
77 
78 #ifndef SET_TT_INCLUDED
79 #include "../rel/set_tt.h"
80 #endif
81 
82 #ifndef REL_N_N_INCLUDED
83 #include "../rel/rel_n_n.h"
84 #endif
85 
86 #ifndef CUR_N_N_INCLUDED
87 #include "../rel/cur_n_n.h"
88 #endif
89 
90 #ifndef INST_INCLUDED
91 #include "inst.h"
92 #endif
93 
94 
95 //=============================================================================
96 // Public Constants:
97 //=============================================================================
98 
99 //-----------------------------------------------------------------------------
100 // BAM_NULL_ADDR: Address to use when the real address is not yet known.
101 //-----------------------------------------------------------------------------
102 
103 static const int BAM_NULL_ADDR = 0;
104 
105 
106 //=============================================================================
107 // Private Constants:
108 //=============================================================================
109 
110 //-----------------------------------------------------------------------------
111 // BAM_MAGIC: Magic number (identifies objects of this class).
112 //-----------------------------------------------------------------------------
113 
114 static const long BAM_MAGIC = 0x41424D0AL; // 'BAM\n'
115 
116 //-----------------------------------------------------------------------------
117 // BAM_CODE_AREA_SIZE: Size of memory for instructions.
118 //-----------------------------------------------------------------------------
119 
120 static const int BAM_CODE_AREA_SIZE = 1024;
121 
122 //-----------------------------------------------------------------------------
123 // BAM_MAX_VARS: Size of memory for variables.
124 //-----------------------------------------------------------------------------
125 
126 static const int BAM_MAX_VARS = 256;
127 
128 //-----------------------------------------------------------------------------
129 // BAM_STACK_SIZE: Size of the stack.
130 //-----------------------------------------------------------------------------
131 
132 static const int BAM_STACK_SIZE = 1024;
133 
134 //-----------------------------------------------------------------------------
135 // BAM_MAX_LIST_2: Maximal number of list_2_c relations with two columns.
136 //-----------------------------------------------------------------------------
137 
138 static const int BAM_MAX_LIST_2 = 10;
139 
140 //-----------------------------------------------------------------------------
141 // BAM_MAX_CUR_2: Maximal number of cur_2_c cursors over lists with two cols.
142 //-----------------------------------------------------------------------------
143 
144 static const int BAM_MAX_CUR_2 = 10;
145 
146 //-----------------------------------------------------------------------------
147 // BAM_MAX_SET_2: Maximal number of set_2_c relations with two columns.
148 //-----------------------------------------------------------------------------
149 
150 static const int BAM_MAX_SET_2 = 10;
151 
152 //-----------------------------------------------------------------------------
153 // BAM_MAX_SET_2B: Maximal number of set_tt_c relations with two columns.
154 //-----------------------------------------------------------------------------
155 
156 static const int BAM_MAX_SET_2B = 10;
157 
158 //-----------------------------------------------------------------------------
159 // BAM_MAX_MMAP_1_1: Maximal number of rel_n_n_c relations with two columns.
160 //-----------------------------------------------------------------------------
161 
162 static const int BAM_MAX_MMAP_1_1 = 10;
163 
164 //-----------------------------------------------------------------------------
165 // BAM_MAX_CUR_1_1: Maximal number cursors for rel_n_n_c relations.
166 //-----------------------------------------------------------------------------
167 
168 static const int BAM_MAX_CUR_1_1 = 10;
169 
170 
171 //=============================================================================
172 // Class for Abstract Machine:
173 //=============================================================================
174 
175 
182 class bam_c {
183  public:
184 
185 //=============================================================================
186 // Constructors, Destructor:
187 //=============================================================================
188 
189 
190  //---------------------------------------------------------------------
191  // Constructor:
192  //---------------------------------------------------------------------
193 
200  explicit bam_c(str_t name);
201 
202 
203  //---------------------------------------------------------------------
204  // Destructor:
205  //---------------------------------------------------------------------
206 
207  ~bam_c();
208 
209 
210  //---------------------------------------------------------------------
211  // Copy-constructor and assignment operator are not supported:
212  //---------------------------------------------------------------------
213 
214  private:
215 
216  bam_c(const bam_c& obj); // Not implemented
217  bam_c& operator=(const bam_c& obj); // Not implemented
218 
219 
220 //=============================================================================
221 // (Object) Methods:
222 //=============================================================================
223 
224  public:
225 
226  //---------------------------------------------------------------------
227  // error:
228  //---------------------------------------------------------------------
229 
234  bool error() {
235 
236  // Check this object:
237  CHECK_VALID("bam_c::err");
238 
239  // Did an error occur?
240  return Error;
241  }
242 
243 
244  //---------------------------------------------------------------------
245  // vars:
246  //---------------------------------------------------------------------
247 
252  void vars(int num_vars);
253 
254 
255  //---------------------------------------------------------------------
256  // string:
257  //---------------------------------------------------------------------
258 
273  int string(str_t str);
274 
275 
276  //---------------------------------------------------------------------
277  // list_2:
278  //---------------------------------------------------------------------
279 
287  int list_2(list_2_t list);
288 
289 
290  //---------------------------------------------------------------------
291  // set_2:
292  //---------------------------------------------------------------------
293 
301  int set_2(set_2_t set);
302 
303  //---------------------------------------------------------------------
304  // set_2b:
305  //---------------------------------------------------------------------
306 
315  int set_2b(set_tt_t set);
316 
317  //---------------------------------------------------------------------
318  // mmap_1_1:
319  //---------------------------------------------------------------------
320 
328  int mmap_1_1(rel_n_n_t mmap);
329 
330 
331  //---------------------------------------------------------------------
332  // cur_2:
333  //---------------------------------------------------------------------
334 
342  int cur_2(int list_id);
343 
344  //---------------------------------------------------------------------
345  // cur_1_1:
346  //---------------------------------------------------------------------
347 
355  int cur_1_1(int mmap_id);
356 
357 
358  //---------------------------------------------------------------------
359  // next_code_addr:
360  //---------------------------------------------------------------------
361 
367 
368  // Check this object:
369  CHECK_VALID("bam_c::next_code_addr");
370 
371  // Address of next instruction:
372  return NextCodeAddr;
373  }
374 
375 
376  //---------------------------------------------------------------------
377  // null:
378  //---------------------------------------------------------------------
379 
384  void null() {
385 
386  // This is an instruction without arguments:
387  store_inst(INST_NULL);
388  }
389 
390 
391  //---------------------------------------------------------------------
392  // halt:
393  //---------------------------------------------------------------------
394 
399  void halt() {
400 
401  // This is an instruction without arguments:
402  store_inst(INST_HALT);
403 
404  }
405 
406  //---------------------------------------------------------------------
407  // call:
408  //---------------------------------------------------------------------
409 
414  void call(int addr) {
415 
416  // Check this object:
417  CHECK_VALID("bam_c::call");
418 
419  // Check parameter:
420  CHECK(addr >= 0, "bam_c::call: addr is negative");
421  CHECK(addr < BAM_CODE_AREA_SIZE,
422  "bam_c::call: addr too large");
423  CHECK(addr < NextCodeAddr,
424  "bam_c::call: addr not used");
425 
426  // Check whether there is space for the instruction:
427  store_inst_16(INST_CALL, addr);
428  }
429 
430 
431  //---------------------------------------------------------------------
432  // call_forward:
433  //---------------------------------------------------------------------
434 
439  void call_forward() {
440 
441  // Check this object:
442  CHECK_VALID("bam_c::call_forward");
443 
444  // Store CALL instruction with null address:
445  store_inst_16(INST_CALL, 0);
446  }
447 
448  //---------------------------------------------------------------------
449  // call_patch:
450  //---------------------------------------------------------------------
451 
456  void call_patch(int inst_addr, int proc_addr) {
457 
458  // Check this object:
459  CHECK_VALID("bam_c::call_patch");
460 
461  // Check parameters:
462  CHECK(inst_addr >= 0,
463  "bam_c::call_patch: inst_addr is negative");
464  CHECK(inst_addr < BAM_CODE_AREA_SIZE,
465  "bam_c::call_patch: inst_addr too large");
466  CHECK(Code[inst_addr] == INST_CALL,
467  "bam_c::call_patch: no CALL instruction");
468  CHECK(proc_addr >= 0,
469  "bam_c::call_patch: proc_addr is negative");
470  CHECK(proc_addr < BAM_CODE_AREA_SIZE,
471  "bam_c::call_patch: proc_addr too large");
472  CHECK(proc_addr <= NextCodeAddr,
473  "bam_c::call_patch: addr not used");
474 
475  // Make sure that jump target has defined instruction:
476  if(proc_addr == NextCodeAddr)
477  store_tmp_halt();
478 
479  // Store address:
480  inst_addr++;
481  Code[inst_addr++] = static_cast<char>((proc_addr>>8) & 0xFF);
482  Code[inst_addr] = static_cast<char>(proc_addr & 0xFF);
483  }
484 
485 
486  //---------------------------------------------------------------------
487  // ret:
488  //---------------------------------------------------------------------
489 
494  void ret() {
495 
496  // This is an instruction without arguments:
497  store_inst(INST_RETURN);
498  }
499 
500 
501  //---------------------------------------------------------------------
502  // dupcheck_2:
503  //---------------------------------------------------------------------
504 
515  void dupcheck_2(int set_id, int var_id) {
516 
517  // Check this object:
518  CHECK_VALID("bam_c::dupcheck_2");
519 
520  // Check parameters:
521  CHECK(set_id >= 0,
522  "bam_c::dupcheck_2: set_id is negative");
523  CHECK(set_id < NumRelSet2,
524  "bam_c::dupcheck_2: set_id too large");
525  CHECK(var_id >= 0,
526  "bam_c::dupcheck_2: var_id is negative");
527  CHECK(var_id < NumVars,
528  "bam_c::dupcheck_2: var_id too large");
529 
530  // This instruction has two 8 bit parameters:
531  store_inst_8_8(INST_DUPCHECK_2, set_id, var_id);
532  }
533 
534 
535  //---------------------------------------------------------------------
536  // dupcheck_2b:
537  //---------------------------------------------------------------------
538 
550  void dupcheck_2b(int set_id, int var1_id, int var2_id) {
551 
552  // Check this object:
553  CHECK_VALID("bam_c::dupcheck_2b");
554 
555  // Check parameters:
556  CHECK(set_id >= 0,
557  "bam_c::dupcheck_2b: set_id is negative");
558  CHECK(set_id < NumRelSet2B,
559  "bam_c::dupcheck_2b: set_id too large");
560  CHECK(var1_id >= 0,
561  "bam_c::dupcheck_2b: var1_id is negative");
562  CHECK(var1_id < NumVars,
563  "bam_c::dupcheck_2b: var1_id too large");
564  CHECK(var2_id >= 0,
565  "bam_c::dupcheck_2b: var2_id is negative");
566  CHECK(var2_id < NumVars,
567  "bam_c::dupcheck_2b: var2_id too large");
568 
569  // This instruction has three 8 bit parameters:
570  store_inst_8_8_8(INST_DUPCHECK_2B, set_id, var1_id, var2_id);
571  }
572 
573 
574  //---------------------------------------------------------------------
575  // save_var:
576  //---------------------------------------------------------------------
577 
585  void save_var(int var_id) {
586 
587  // Check this object:
588  CHECK_VALID("bam_c::save_var");
589 
590  // Check parameters:
591  CHECK(var_id >= 0,
592  "bam_c::save_var: var_id is negative");
593  CHECK(var_id < NumVars,
594  "bam_c::save_var: var_id too large");
595 
596  // This instruction has one 8 bit parameter:
597  store_inst_8(INST_SAVE_VAR, var_id);
598  }
599 
600 
601  //---------------------------------------------------------------------
602  // restore_var:
603  //---------------------------------------------------------------------
604 
612  void restore_var(int var_id) {
613 
614  // Check this object:
615  CHECK_VALID("bam_c::restore_var");
616 
617  // Check parameters:
618  CHECK(var_id >= 0,
619  "bam_c::restore_var: var_id is negative");
620  CHECK(var_id < NumVars,
621  "bam_c::restore_var: var_id too large");
622 
623  // This instruction has one 8 bit parameter:
624  store_inst_8(INST_RESTORE_VAR, var_id);
625  }
626 
627 
628  //---------------------------------------------------------------------
629  // save_mmap_1_1_cur:
630  //---------------------------------------------------------------------
631 
636  void save_mmap_1_1_cur(int cur_id) {
637 
638  // Check this object:
639  CHECK_VALID("bam_c::save_mmap_1_1_cur");
640 
641  // Check parameter:
642  CHECK(cur_id >= 0,
643  "bam_c::save_mmap_1_1_cur: cur_id is negative");
644  CHECK(cur_id < NumCurMMap11,
645  "bam_c::save_mmap_1_1_cur: cur_id too large");
646 
647  // This instruction has one 8 bit argument:
648  store_inst_8(INST_SAVE_MMAP_1_1_CUR, cur_id);
649  }
650 
651 
652  //---------------------------------------------------------------------
653  // restore_mmap_1_1_cur:
654  //---------------------------------------------------------------------
655 
660  void restore_mmap_1_1_cur(int cur_id) {
661 
662  // Check this object:
663  CHECK_VALID("bam_c::restore_mmap_1_1_cur");
664 
665  // Check parameter:
666  CHECK(cur_id >= 0,
667  "bam_c::restore_mmap_1_1_cur: cur_id is negative");
668  CHECK(cur_id < NumCurMMap11,
669  "bam_c::restore_mmap_1_1_cur: cur_id too large");
670 
671  // This instruction has one 8 bit argument:
672  store_inst_8(INST_RESTORE_MMAP_1_1_CUR, cur_id);
673  }
674 
675  //---------------------------------------------------------------------
676  // loop_list_2:
677  //---------------------------------------------------------------------
678 
684  void loop_list_2(int cur_id) {
685 
686  // Check this object:
687  CHECK_VALID("bam_c::loop_list_2");
688 
689  // Check parameter:
690  CHECK(cur_id >= 0, "bam_c::loop_list_2: cur_id is negative");
691  CHECK(cur_id < NumCurList2,
692  "bam_c::loop_list_2: cur_id too large");
693 
694  // This instruction has one 8 bit and one (unknown) 16 bit arg:
695  store_inst_8_16(INST_LOOP_LIST_2, cur_id, 0);
696  }
697 
698 
699  //---------------------------------------------------------------------
700  // loop_patch:
701  //---------------------------------------------------------------------
702 
707  void loop_patch(int inst_addr, int loop_done_addr) {
708 
709  // Check this object:
710  CHECK_VALID("bam_c::loop_patch");
711 
712  // Check parameters:
713  CHECK(inst_addr >= 0,
714  "bam_c::loop_patch: inst_addr is negative");
715  CHECK(inst_addr < BAM_CODE_AREA_SIZE,
716  "bam_c::loop_patch: inst_addr too large");
717  CHECK(inst_addr < NextCodeAddr,
718  "bam_c::loop_patch: inst_addr not used");
719  CHECK(Code[inst_addr] == INST_LOOP_LIST ||
720  Code[inst_addr] == INST_LOOP_LIST_2 ||
721  Code[inst_addr] == INST_LOOP_MMAP ||
722  Code[inst_addr] == INST_LOOP_MMAP_1_1,
723  "bam_c::loop_patch: no LOOP instruction");
724  CHECK(loop_done_addr >= 0,
725  "bam_c::loop_patch: loop_done_addr is negative");
726  CHECK(loop_done_addr < BAM_CODE_AREA_SIZE,
727  "bam_c::loop_patch: loop_done_addr too large");
728  CHECK(loop_done_addr <= NextCodeAddr,
729  "bam_c::loop_patch: loop_done_addr not used");
730 
731  // Make sure that jump target has defined instruction:
732  if(loop_done_addr == NextCodeAddr)
733  store_tmp_halt();
734 
735  // Store address:
736  inst_addr++; // For OpCode
737  inst_addr++; // For Cursor ID
738  Code[inst_addr++] = static_cast<char>((loop_done_addr>>8)&0xFF);
739  Code[inst_addr] = static_cast<char>(loop_done_addr & 0xFF);
740  }
741 
742  //---------------------------------------------------------------------
743  // loop_mmap_1_1:
744  //---------------------------------------------------------------------
745 
762  void loop_mmap_1_1(int cur_id, int loop_done_addr, int var_id) {
763 
764  // Check this object:
765  CHECK_VALID("bam_c::loop_mmap_1_1");
766 
767  // Check parameter:
768  CHECK(cur_id >= 0,
769  "bam_c::loop_mmap_1_1: cur_id is negative");
770  CHECK(cur_id < NumCurMMap11,
771  "bam_c::loop_mmap_1_1: cur_id too large");
772  CHECK(loop_done_addr == BAM_NULL_ADDR ||
773  loop_done_addr >= 0,
774  "bam_c::loop_mmap_1_1: loop_done_addr is negative");
775  CHECK(loop_done_addr == BAM_NULL_ADDR ||
776  loop_done_addr < BAM_CODE_AREA_SIZE,
777  "bam_c::loop_mmap_1_1: loop_done_addr too large");
778  CHECK(loop_done_addr == BAM_NULL_ADDR ||
779  loop_done_addr <= NextCodeAddr,
780  "bam_c::loop_mmap_1_1: loop_done_addr not used");
781  CHECK(var_id >= 0,
782  "bam_c::loop_mmap_1_1: var_id is negative");
783  CHECK(var_id < NumVars,
784  "bam_c::loop_mmap_1_1: var_id too large");
785 
786  // The instruction has one 8 bit, one 16 bit, and one 8 bit arg:
787  store_inst_8_16_8(INST_LOOP_MMAP_1_1, cur_id, loop_done_addr,
788  var_id);
789  }
790 
791  //---------------------------------------------------------------------
792  // get_list_2_col_1:
793  //---------------------------------------------------------------------
794 
799  void get_list_2_col_1(int cur_id, int var_id) {
800 
801  // Check this object:
802  CHECK_VALID("bam_c::get_list_2_col_1");
803 
804  // Check parameters:
805  CHECK(cur_id >= 0,
806  "bam_c::get_list_2_col_1: cur_id is negative");
807  CHECK(cur_id < NumCurList2,
808  "bam_c::get_list_2_col_1: cur_id too large");
809  CHECK(var_id >= 0,
810  "bam_c::get_list_2_col_1: var_id is negative");
811  CHECK(var_id < NumVars,
812  "bam_c::get_list_2_col_1: var_id too large");
813 
814  // This is an instruction with two 8 bit arguments:
815  store_inst_8_8(INST_GET_LIST_2_COL_1, cur_id, var_id);
816  }
817 
818 
819  //---------------------------------------------------------------------
820  // get_list_2_col_2:
821  //---------------------------------------------------------------------
822 
827  void get_list_2_col_2(int cur_id, int var_id) {
828 
829  // Check this object:
830  CHECK_VALID("bam_c::get_list_2_col_2");
831 
832  // Check parameters:
833  CHECK(cur_id >= 0,
834  "bam_c::get_list_2_col_2: cur_id is negative");
835  CHECK(cur_id < NumCurList2,
836  "bam_c::get_list_2_col_2: cur_id too large");
837  CHECK(var_id >= 0,
838  "bam_c::get_list_2_col_2: var_id is negative");
839  CHECK(var_id < NumVars,
840  "bam_c::get_list_2_col_2: var_id too large");
841 
842  // This is an instruction with two 8 bit arguments:
843  store_inst_8_8(INST_GET_LIST_2_COL_2, cur_id, var_id);
844  }
845 
846 
847  //---------------------------------------------------------------------
848  // get_mmap_1_1_out_1:
849  //---------------------------------------------------------------------
850 
855  void get_mmap_1_1_out_1(int cur_id, int var_id) {
856 
857  // Check this object:
858  CHECK_VALID("bam_c::get_mmap_1_1_out_1");
859 
860  // Check parameters:
861  CHECK(cur_id >= 0,
862  "bam_c::get_mmap_1_1_out_1: cur_id is negative");
863  CHECK(cur_id < NumCurMMap11,
864  "bam_c::get_mmap_1_1_out_1: cur_id too large");
865  CHECK(var_id >= 0,
866  "bam_c::get_mmap_1_1_out_1: var_id is negative");
867  CHECK(var_id < NumVars,
868  "bam_c::get_mmap_1_1_out_1: var_id too large");
869 
870  // This is an instruction with two 8 bit arguments:
871  store_inst_8_8(INST_GET_MMAP_1_1_OUT_1, cur_id, var_id);
872  }
873 
874 
875  //---------------------------------------------------------------------
876  // end_loop_list_2:
877  //---------------------------------------------------------------------
878 
894  void end_loop_list_2(int cur_id, int loop_body_addr) {
895 
896  // Check this object:
897  CHECK_VALID("bam_c::end_loop_list_2");
898 
899  // Check parameters:
900  CHECK(cur_id >= 0,
901  "bam_c::end_loop_list_2: cur_id is negative");
902  CHECK(cur_id < NumCurList2,
903  "bam_c::end_loop_list_2: cur_id too large");
904  CHECK(loop_body_addr >= 0,
905  "bam_c::end_loop_list_2: loop_body_addr is negative");
906  CHECK(loop_body_addr < BAM_CODE_AREA_SIZE,
907  "bam_c::end_loop_list_2: loop_body_addr too large");
908  CHECK(loop_body_addr < NextCodeAddr,
909  "bam_c::end_loop_list_2: loop_body_addr not used");
910 
911  // This instruction has an 8 bit and a 16 bit parameter:
912  store_inst_8_16(INST_END_LOOP_LIST_2, cur_id, loop_body_addr);
913  }
914 
915 
916  //---------------------------------------------------------------------
917  // end_loop_mmap_1_1:
918  //---------------------------------------------------------------------
919 
935  void end_loop_mmap_1_1(int cur_id, int loop_body_addr) {
936 
937  // Check this object:
938  CHECK_VALID("bam_c::end_loop_mmap_1_1");
939 
940  // Check parameters:
941  CHECK(cur_id >= 0,
942  "bam_c::end_loop_mmap_1_1: cur_id is negative");
943  CHECK(cur_id < NumCurMMap11,
944  "bam_c::end_loop_mmap_1_1: cur_id too large");
945  CHECK(loop_body_addr >= 0,
946  "bam_c::end_loop_mmap_1_1: loop_body_addr is negative");
947  CHECK(loop_body_addr < BAM_CODE_AREA_SIZE,
948  "bam_c::end_loop_mmap_1_1: loop_body_addr too large");
949  CHECK(loop_body_addr < NextCodeAddr,
950  "bam_c::end_loop_mmap_1_1: loop_body_addr not used");
951 
952  // This instruction has an 8 bit and a 16 bit parameter:
953  store_inst_8_16(INST_END_LOOP_MMAP_1_1, cur_id, loop_body_addr);
954  }
955 
956 
957  //---------------------------------------------------------------------
958  // insert_list_2:
959  //---------------------------------------------------------------------
960 
973  void insert_list_2(int rel_id, int var1_id, int var2_id) {
974 
975  // Check this object:
976  CHECK_VALID("bam_c::insert_list_2");
977 
978  // Check parameters:
979  CHECK(rel_id >= 0,
980  "bam_c::insert_list_2: rel_id is negative");
981  CHECK(rel_id < NumRelList2,
982  "bam_c::insert_list_2: rel_id too large");
983  CHECK(var1_id >= 0,
984  "bam_c::insert_list_2: var1_id is negative");
985  CHECK(var1_id < NumVars,
986  "bam_c::insert_list_2: var1_id too large");
987  CHECK(var2_id >= 0,
988  "bam_c::insert_list_2: var2_id is negative");
989  CHECK(var2_id < NumVars,
990  "bam_c::insert_list_2: var2_id too large");
991 
992  // This is an instruction with two 8 bit arguments:
993  store_inst_8_8_8(INST_INSERT_LIST_2, rel_id, var1_id, var2_id);
994  }
995 
996 
997  //---------------------------------------------------------------------
998  // insert_list_2_v:
999  //---------------------------------------------------------------------
1000 
1005  void insert_list_2_v(int rel_id, int var_id) {
1006 
1007  // Check this object:
1008  CHECK_VALID("bam_c::insert_list_2_v");
1009 
1010  // Check parameters:
1011  CHECK(rel_id >= 0,
1012  "bam_c::insert_list_2_v: rel_id is negative");
1013  CHECK(rel_id < NumRelList2,
1014  "bam_c::insert_list_2_v: rel_id too large");
1015  CHECK(var_id >= 0,
1016  "bam_c::insert_list_2_v: var_id is negative");
1017  CHECK(var_id < NumVars,
1018  "bam_c::insert_list_2_v: var_id too large");
1019 
1020  // This is an instruction with two 8 bit arguments:
1021  store_inst_8_8(INST_INSERT_LIST_2_V, rel_id, var_id);
1022  }
1023 
1024 
1025  //---------------------------------------------------------------------
1026  // insert_mmap_1_1:
1027  //---------------------------------------------------------------------
1028 
1041  void insert_mmap_1_1(int set_id, int var1_id, int var2_id) {
1042 
1043  // Check this object:
1044  CHECK_VALID("bam_c::insert_mmap_1_1");
1045 
1046  // Check parameters:
1047  CHECK(set_id >= 0,
1048  "bam_c::insert_mmap_1_1: set_id is negative");
1049  CHECK(set_id < NumRelMMap11,
1050  "bam_c::insert_mmap_1_1: set_id too large");
1051  CHECK(var1_id >= 0,
1052  "bam_c::insert_mmap_1_1: var1_id is negative");
1053  CHECK(var1_id < NumVars,
1054  "bam_c::insert_mmap_1_1: var1_id too large");
1055  CHECK(var2_id >= 0,
1056  "bam_c::insert_mmap_1_1: var2_id is negative");
1057  CHECK(var2_id < NumVars,
1058  "bam_c::insert_mmap_1_1: var2_id too large");
1059 
1060  // This instruction has three 8 bit parameters:
1061  store_inst_8_8_8(INST_INSERT_MMAP_1_1,
1062  set_id, var1_id, var2_id);
1063  }
1064 
1065 
1066  //---------------------------------------------------------------------
1067  // insert_mmap_1_1:
1068  //---------------------------------------------------------------------
1069 
1082  void trace_call_2(int str_id, int var1_id, int var2_id) {
1083 
1084  // Check this object:
1085  CHECK_VALID("bam_c::trace_call_2");
1086 
1087  // Check parameters:
1088  CHECK(str_id >= 0,
1089  "bam_c::trace_call_2: str_id is negative");
1090  CHECK(str_id < Strings.length(),
1091  "bam_c::trace_call_2: str_id too large");
1092  CHECK(str_id < 256,
1093  "bam_c::trace_call_2: str_id not 8 bit");
1094  CHECK(var1_id >= 0,
1095  "bam_c::trace_call_2: var1_id is negative");
1096  CHECK(var1_id < NumVars,
1097  "bam_c::trace_call_2: var1_id too large");
1098  CHECK(var2_id >= 0,
1099  "bam_c::trace_call_2: var2_id is negative");
1100  CHECK(var2_id < NumVars,
1101  "bam_c::trace_call_2: var2_id too large");
1102 
1103  // This instruction has three 8 bit parameters:
1104  store_inst_8_8_8(INST_TRACE_CALL_2,
1105  str_id, var1_id, var2_id);
1106  }
1107 
1108 
1109  //---------------------------------------------------------------------
1110  // run:
1111  //---------------------------------------------------------------------
1112 
1117  bool run(int start_addr);
1118 
1119 
1120 
1121 //=============================================================================
1122 // Debugging Support:
1123 //=============================================================================
1124 
1125  //---------------------------------------------------------------------
1126  // check:
1127  //---------------------------------------------------------------------
1128 
1129 #if VER_DEBUG
1130 
1135  public:
1136  str_t check() const;
1137 
1138  // Magic number (identifies objects of this class for debugging).
1139  private:
1140  long Magic; // Must be "BAM_MAGIC".
1141 #endif
1142 
1143  //---------------------------------------------------------------------
1144  // dump:
1145  //---------------------------------------------------------------------
1146 
1147 #if VER_DUMP
1148 
1153  public:
1154  void dump(str_t headline = STR_NULL) const;
1155 #endif
1156 
1157 //=============================================================================
1158 // Auxiliary Methods:
1159 //=============================================================================
1160 
1161  private:
1162 
1163  //---------------------------------------------------------------------
1164  // store_inst:
1165  //---------------------------------------------------------------------
1166 
1171  void store_inst(inst_t inst);
1172 
1173 
1174  //---------------------------------------------------------------------
1175  // store_inst_8:
1176  //---------------------------------------------------------------------
1177 
1182  void store_inst_8(inst_t inst, int arg);
1183 
1184 
1185  //---------------------------------------------------------------------
1186  // store_inst_16:
1187  //---------------------------------------------------------------------
1188 
1193  void store_inst_16(inst_t inst, int arg);
1194 
1195 
1196  //---------------------------------------------------------------------
1197  // store_inst_8_8:
1198  //---------------------------------------------------------------------
1199 
1204  void store_inst_8_8(inst_t inst, int arg1, int arg2);
1205 
1206 
1207  //---------------------------------------------------------------------
1208  // store_inst_8_8_8:
1209  //---------------------------------------------------------------------
1210 
1215  void store_inst_8_8_8(inst_t inst, int arg1, int arg2, int arg3);
1216 
1217 
1218  //---------------------------------------------------------------------
1219  // store_inst_8_16:
1220  //---------------------------------------------------------------------
1221 
1226  void store_inst_8_16(inst_t inst, int arg1, int arg2);
1227 
1228 
1229  //---------------------------------------------------------------------
1230  // store_inst_8_16_8:
1231  //---------------------------------------------------------------------
1232 
1237  void store_inst_8_16_8(inst_t inst, int arg1, int arg2, int arg3);
1238 
1239 
1240  //---------------------------------------------------------------------
1241  // store_tmp_halt:
1242  //---------------------------------------------------------------------
1243 
1256  void store_tmp_halt();
1257 
1258 
1259 //=============================================================================
1260 // Private Object Members:
1261 //=============================================================================
1262 
1263  private:
1264 
1265  // Memory Pool for Allocating Storage Pages:
1266  mpool_c MemPool;
1267 
1268  // Rest of current memory page for allocating relations and cursors:
1269  page_u_t FreeMemPtr;
1270 
1271  // Number of page_u units still available in the current memory page:
1272  int FreeMemUnits;
1273 
1274  // Did an error occur?
1275  bool Error;
1276 
1277  // Code area (memory for instructions):
1278  unsigned char Code[BAM_CODE_AREA_SIZE];
1279 
1280  // Next free location in code area:
1281  int NextCodeAddr;
1282 
1283  // Memory for variables:
1284  int Vars[BAM_MAX_VARS];
1285 
1286  // Number of variables that are actually used:
1287  int NumVars;
1288 
1289  // Stack:
1290  //stack_c<int,BAM_STACK_SIZE> Stack;
1291  stack_c<int> Stack;
1292 
1293  // String memory:
1294  strmem_c StrMem;
1295  flexarr_c<str_t> Strings;
1296 
1297  // Lists with two columns:
1298  list_2_t RelList2[BAM_MAX_LIST_2];
1299  int NumRelList2;
1300 
1301  // Cursors over list_2_c relations:
1302  cur_2_t CurList2[BAM_MAX_CUR_2];
1303  int NumCurList2;
1304 
1305  // Sets with two columns:
1306  set_2_t RelSet2[BAM_MAX_SET_2];
1307  int NumRelSet2;
1308 
1309  // Sets with two columns, bitmap version:
1310  set_tt_t RelSet2B[BAM_MAX_SET_2B];
1311  int NumRelSet2B;
1312 
1313  // MultiMaps with one column mapped to one column:
1314  rel_n_n_t RelMMap11[BAM_MAX_MMAP_1_1];
1315  int NumRelMMap11;
1316 
1317  // Cursors over multimaps with one column mapped to one column:
1318  cur_n_n_t CurMMap11[BAM_MAX_CUR_1_1];
1319  int NumCurMMap11;
1320 
1321 
1322 //=============================================================================
1323 // End of Class:
1324 //=============================================================================
1325 
1326 };
1327 
1328 //-----------------------------------------------------------------------------
1329 // Define pointer type:
1330 //-----------------------------------------------------------------------------
1331 
1336 typedef bam_c *bam_t;
1337 
1338 //-----------------------------------------------------------------------------
1339 // Define null pointer:
1340 //-----------------------------------------------------------------------------
1341 
1346 #define BAM_NULL (static_cast<bam_t>(0))
1347 
1348 //=============================================================================
1349 // End of Include File:
1350 //=============================================================================
1351 
1352 #endif
1353 
void get_list_2_col_2(int cur_id, int var_id)
Definition: bam.h:827
void save_mmap_1_1_cur(int cur_id)
Definition: bam.h:636
void end_loop_mmap_1_1(int cur_id, int loop_body_addr)
Definition: bam.h:935
void get_list_2_col_1(int cur_id, int var_id)
Definition: bam.h:799
void dump(str_t headline=STR_NULL) const
Definition: bam.cpp:1235
int cur_1_1(int mmap_id)
Definition: bam.cpp:316
int list_2(list_2_t list)
Definition: bam.cpp:160
void end_loop_list_2(int cur_id, int loop_body_addr)
Definition: bam.h:894
void call_forward()
Definition: bam.h:439
void loop_patch(int inst_addr, int loop_done_addr)
Definition: bam.h:707
void dupcheck_2b(int set_id, int var1_id, int var2_id)
Definition: bam.h:550
void restore_mmap_1_1_cur(int cur_id)
Definition: bam.h:660
int next_code_addr()
Definition: bam.h:366
Definition: mpool.h:103
void insert_list_2(int rel_id, int var1_id, int var2_id)
Definition: bam.h:973
Definition: cur_n_n.h:68
void dupcheck_2(int set_id, int var_id)
Definition: bam.h:515
void loop_list_2(int cur_id)
Definition: bam.h:684
int string(str_t str)
Definition: bam.cpp:127
Definition: list_2.h:58
bam_c(str_t name)
Definition: bam.cpp:55
void vars(int num_vars)
Definition: bam.cpp:104
#define CHECK_VALID(EX)
Definition: check.h:85
Definition: strmem.h:65
void null()
Definition: bam.h:384
const char * str_t
Definition: str.h:41
void loop_mmap_1_1(int cur_id, int loop_done_addr, int var_id)
Definition: bam.h:762
int cur_2(int list_id)
Definition: bam.cpp:263
void insert_mmap_1_1(int set_id, int var1_id, int var2_id)
Definition: bam.h:1041
void get_mmap_1_1_out_1(int cur_id, int var_id)
Definition: bam.h:855
void trace_call_2(int str_id, int var1_id, int var2_id)
Definition: bam.h:1082
Definition: bam.h:182
Definition: page.h:78
Definition: set_tt.h:121
void call(int addr)
Definition: bam.h:414
int mmap_1_1(rel_n_n_t mmap)
Definition: bam.cpp:238
#define STR_NULL
Definition: str.h:52
void halt()
Definition: bam.h:399
Definition: rel_n_n.h:78
void restore_var(int var_id)
Definition: bam.h:612
bam_c * bam_t
Definition: bam.h:1336
void insert_list_2_v(int rel_id, int var_id)
Definition: bam.h:1005
int set_2(set_2_t set)
Definition: bam.cpp:186
int set_2b(set_tt_t set)
Definition: bam.cpp:212
enum inst_enum inst_t
Definition: cur_2.h:62
void ret()
Definition: bam.h:494
bool run(int start_addr)
Definition: bam.cpp:370
bool error()
Definition: bam.h:234
#define CHECK(EX, MSG)
Definition: check.h:69
void call_patch(int inst_addr, int proc_addr)
Definition: bam.h:456
Definition: set_2.h:58
Enumeration type for Abstract Machine Instructions (OpCodes)
void save_var(int var_id)
Definition: bam.h:585