home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / brklyprl.lha / Emulator / inst_args.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-04-14  |  4.4 KB  |  207 lines

  1.  
  2. /* Copyright (C) 1988, 1989 Herve' Touati, Aquarius Project, UC Berkeley */
  3.  
  4. /* Copyright Herve' Touati, Aquarius Project, UC Berkeley */
  5.  
  6. #include <stream.h>
  7. #include <ctype.h>
  8. #include "tags.h"
  9. #include "instr.h"
  10. #include "hash_table.h"
  11. #include "string_table.h"
  12. #include "scan.h"
  13. #include "inst_args.h"
  14. #include "memory.h"
  15. #include "basics.h"
  16.  
  17. InstrArg* instr_args[ARG_LAST];
  18.  
  19. void init_instr_args(Scan& scanner) {
  20.   int i = 0;
  21. #define use(String,ARGID,NAME)\
  22.     instr_args[i] = new NAME;\
  23.     instr_args[i]->init(scanner);\
  24.     instr_args[i++]->name = String;
  25. #include "list_inst_args.h"
  26. #undef use
  27. }
  28.  
  29. extern int atoi(const char*);
  30.  
  31. int Var::fill(char* s)
  32. {
  33.   return atoi(++s) - 1;
  34. }
  35.  
  36. int Label::fill(char* s)
  37. {
  38.   return (*s == '_') ? atoi(++s) : LABEL_FAIL;
  39. }
  40.  
  41. void Label::print(int value)
  42. {
  43.     if (instrp(value) == FP0)
  44.       cout << "fail";
  45.     else
  46.       printf("_%d", instrp(value) - P0 + 1);
  47. }
  48.  
  49. int Label::update(int label)
  50. {
  51.   int addr = label_table->get(label);
  52.   return (int) ((label_table->status == HASH_MISS) ? FP0 : P0 + addr -1);
  53. }
  54.  
  55. void Label::init(Scan&)
  56. {
  57.   label_table = new HashTable;
  58. }
  59.  
  60. int Int::fill(char* s)
  61. {
  62.   return atoi(s);
  63. }
  64.  
  65. int Const::fill(char* s)
  66. {
  67.   Cell Val;
  68.   if (*s == '&' && isdigit(*(s + 1))) {
  69.     Val = make_int(atoi(++s));
  70.   } else {
  71.     Val = make_atom(symbol_table->intern(s));
  72.   }
  73.   return Val;
  74. }
  75.  
  76. void Const::print(int value) 
  77. {
  78.   if (is_atom(value))
  79.     cout << get_string(value);
  80.   else 
  81.     cout << "&" << get_int(value);
  82. }
  83.  
  84. void BuiltIn::init(Scan& ST)
  85. {
  86.   name_to_ID = new HashTable;
  87.   exec_to_ID = new HashTable;
  88.   built_in_table = new BuiltInEntry[LAST_BUILT_IN];
  89.   symbol_table = &ST;
  90. #define NAMES
  91. #define use(ID,NAME,FUNCTION) {\
  92.   int value = symbol_table->intern(NAME);\
  93.   name_to_ID->bind(value, ID);\
  94.   built_in_table[ID].name = value;\
  95.   extern void FUNCTION();\
  96.   built_in_table[ID].exec = &FUNCTION;\
  97.   exec_to_ID->bind((int) &FUNCTION, ID);\
  98. }
  99. #include "built_ins.h"
  100. #undef use
  101. #undef NAMES  
  102. }
  103.  
  104. PF BuiltIn::get_exec(int name)
  105. {
  106.   int ID = name_to_ID->get(name);
  107.   if (name_to_ID->status == HASH_MISS) return 0;
  108.   return built_in_table[ID].exec;
  109. }
  110.  
  111. void Table::print(int value)
  112. {
  113.   HashTable* table = (HashTable*) TableOfTables->get(value);
  114.   table->reset();
  115.   HashTableEntry* e;
  116.   while (e = table->next()) {
  117.     cout << "\n\t";
  118.     instr_args[ARG_CONST]->print(e->key);
  119.     cout << " -> ";
  120.     instr_args[ARG_LABEL]->print(e->value);
  121.   }
  122. }
  123.  
  124. int Table::fill(char* s)
  125. {
  126.   Int to_size;
  127.   int table_size = (to_size.fill(s) + 1)/ 2;
  128.   HashTable* table = new HashTable(table_size + 2);
  129.  
  130.  /* first, get rid of the table label */
  131.   scanner->next_line();
  132.  
  133.  /* the main loop */
  134.  /* can continue as long as no tcdr sign is encountered */
  135.   int can_continue = 1;
  136.   for (int i = 0; can_continue && i < table_size; i++) {
  137.     scanner->next_line();
  138.     int constant = instr_args[ARG_CONST]->fill(scanner->p[0]);
  139.     can_continue = (*scanner->p[1] == '\0');
  140.     scanner->next_line();
  141.     int label = instr_args[ARG_LABEL]->fill(scanner->p[0]);
  142.     label = instr_args[ARG_LABEL]->update(label);
  143.     table->bind(constant, label);
  144.   }
  145.  
  146.  /* scan the entries up to the end */
  147.   for (; i < table_size; i++) {
  148.     scanner->next_line();
  149.     scanner->next_line();
  150.   }
  151.   scanner->status = SCAN_INSTR;
  152.   TableOfTables->bind(table_count, (int) table);
  153.   return table_count++;
  154. }
  155.  
  156. HashTable* Proc::get_table()
  157. {
  158.   return proc_name_table;
  159. }
  160.  
  161. int Proc::update(int name)
  162. {
  163.   int addr = proc_addr_table->get(name);
  164.   if (proc_addr_table->status == HASH_MISS) {
  165.     status = UNRESOLVED_ADDRESSES;
  166.     return 0;
  167.   }
  168.   return (int) (P0 + addr -1);
  169. }
  170.  
  171. int Proc::get_arity(int addr)
  172. {
  173.   addr = instrp(addr) - P0 + 1;
  174.   int name = proc_name_table->get(addr);
  175.   return symbol_table->get_arity(name);
  176. }
  177.  
  178. void Proc::print(int addr)
  179. {
  180.   if (addr == 0) {printf("-???-"); return;}
  181.   addr = instrp(addr) - P0 + 1;
  182.   int name = proc_name_table->get(addr);
  183.   int arity = symbol_table->get_arity(name);
  184.   printf("%s(#%d,@%d)", (char*) name, arity, addr);
  185. }
  186.  
  187. void Proc::print_proc_table() 
  188. {
  189.   proc_name_table->reset();
  190.   HashTableEntry* e;
  191.   while (e = proc_name_table->next()) {
  192.     cout << "\n\t";
  193.     print((int) (P0 + e->key - 1));
  194.   }
  195.   cout << "\n";
  196. }
  197.  
  198. /* proc_addr_table: key->name, value->addr(as offset in code space) */
  199.  /* proc_name_table: key->addr(as offset in code space), value->name */
  200. void Proc::init(Scan& ST)
  201. {
  202.   symbol_table = &ST;
  203.   proc_addr_table = new HashTable;
  204.   proc_name_table = new HashTable;
  205.   status = NO_UNRESOLVED_ADDRESSES;
  206. }
  207.