/************************************************************************ * * * The SB-Prolog System * * Copyright SUNY at Stony Brook, 1986 * * * ************************************************************************/ /*----------------------------------------------------------------- SB-Prolog is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY. No author or distributor accepts responsibility to anyone for the consequences of using it or for whether it serves any particular purpose or works at all, unless he says so in writing. Refer to the SB-Prolog General Public License for full details. Everyone is granted permission to copy, modify and redistribute SB-Prolog, but only under the conditions described in the SB-Prolog General Public License. A copy of this license is supposed to have been given to you along with SB-Prolog so you can know your rights and responsibilities. It should be in a file named COPYING. Among other things, the copyright notice and this notice must be preserved on all copies. ------------------------------------------------------------------ */ /* loader.c */ #include "sim.h" #include "inst.h" #include "aux.h" #define get_data(x,y) (y - fread(x, sizeof(*x), y, fp) ) #define st_ptrptrpsc(i_addr) *((pw *)i_addr) = reloc_table[*(pw)i_addr]; #define st_ptrpsc(i_addr) *((pw)i_addr) = *reloc_table[*(pw)i_addr]; static pb inst_addr, hptr; static pw reloc_table[500]; static byte *last_text; unsigned long eof_flag; unsigned long psc_bytes, text_bytes, index_bytes, magic; int (*load_routine[16])(); static FILE *fp; static byte perm = PERM; int inst_length[15] = { 0, 4, 6, 6, 2, 6, 6, 6, 6, 10, 6, 6, 2, 4, 10}; extern fread(); /****************************************************************************/ /* */ /* fix_bb: fixes the byte-backwards problem. It is passed a pointer to a */ /* sequence of 4 bytes read in from a file as bytes. It then converts those */ /* bytes to represent a number. This code works for any machine, and makes */ /* the byte-code machine independent. */ /* */ /****************************************************************************/ fix_bb(lptr) unsigned char *lptr; { unsigned long *numptr; numptr = (unsigned long *)lptr; *numptr = (((((*lptr << 8) | *(lptr+1)) << 8) | *(lptr+2)) << 8) | *(lptr+3); } /****************************************************************************/ /* */ /* Load the file into permanent space, starting from "curr_fence". */ /* Data segment first (mixed psc entries and name strings), then text */ /* segment, ended with a virtual instruction "endfile " where the */ /* pointer is a pointer to the next text segment (of another byte code */ /* file). */ /* */ /****************************************************************************/ loader(file) char *file; { byte *restore; int err_msg; fp = fopen(file, "r"); if ( fp == NULL ) return 10; if (hitrace) printf("\n ...... loading file %s\n", file); while ((eof_flag = get_data(&magic, 1)) == 0) { if (eof_flag = get_data(&psc_bytes, 1)) return ( 1 ); if (eof_flag = get_data(&text_bytes, 1)) return ( 1 ); if (eof_flag = get_data(&index_bytes, 1)) return ( 1 ); err_msg = load_syms() ; if ( err_msg != 0 ) { printf("error %d loading file %s\n", err_msg, file); exit(1); /* eventually upper level routines will determine */ } else { restore = curr_fence; err_msg = load_text(); if (err_msg != 0) { printf("error %d loading file %s\n", err_msg, file); curr_fence = restore; exit(1); /* eventually upper level routines will determine */ } else { err_msg = load_index(); if (err_msg != 0) { printf("error in loading file %s\n", err_msg, file); curr_fence = restore; exit(1); /*eventually upper level routines will determine */ } else { if (eof_flag = get_data(inst_addr, 6)) return (eof_flag); if (*inst_addr != endfile) *inst_addr = endfile; inst_addr += 6; *((pw)(inst_addr-4)) = 0; last_text = inst_addr - 4; curr_fence = inst_addr; if (curr_fence > max_fence) quit("Program area overflow\n"); } } } } fclose(fp); return 0; } /************************************************************************ * * * Load_syms is a function which loads a symbol table given in a byte * * code file into an appropriate format in the pcs table. As part of * * its function it resolves entry points for byte code intructions (call * * to relloc_addr), and maintains a tableau so that instructions * * with indexes into the psc table may have those indexex resloved before* * loading them in the intruction array (byte code program space). The * * intructions are loaded by a separate function. * * The function returns a short integer which is a error code. Relevant * * error codes are listed below. * * * * 1: in load_syms: incomplete or missing psc count * * 2: in load_syms: incomplete or missing ep in record dec * * 3: in load_syms: incomplete or missing arity in record dec * * 4: in load_syms: incomplete or missing length in record dec * * 5: in load_syms: incomplete or missing name in record dec * * 8: can not open file * * * ************************************************************************/ load_syms() { char name[256]; int ep_offset; byte *reloc_addr( ); unsigned long i, j, count; byte temp_len; byte temp_arity; word *insert ( ); i = 0; count = 0; fix_bb(&psc_bytes); /* caller read psc_bytes, we just fix it */ while ( (count < psc_bytes) && (eof_flag == 0) ) { if ( eof_flag = get_data(&ep_offset, 1) ) return(2); fix_bb(&ep_offset); if ( eof_flag = get_data(&temp_arity,1) ) return(3); if ( eof_flag = get_data(&temp_len,1) ) return(4); if ( eof_flag = get_data(name, temp_len) ) return(5); reloc_table[i] = insert(name, temp_len, temp_arity, &perm); set_temp_ep(*reloc_table[i], ep_offset); count += temp_len + 6; i++; } for (j = 0; j < i; j++) { set_real_ep(*reloc_table[j], curr_fence); } pspace_used = ((int)curr_fence - (int)(pspace))/4; return(0); } /* load_syms */ /************************************************************************ * * * Load_text loads the byte code intruction from a byte code file to * * the byte code program space. References to indexes to the pcs table * * are resolved with the use of the macro st_index. New index relies * * on the symbol table array which is assigned values by load_syms. * * The routine assumes the current length 8/18/84 of byte code * * intructions when reading from the byte code file. * * The function returns a short integer which is a error code. Relevant * * error codes are listed below. * * * * 6: in load_text: byte code Operands are non-existent * * 7: in load_text: Illegal instruction from PIL file * * 8: can not open file * * * ************************************************************************/ load_text () { long current_opcode = 0; long count = 0; byte *reloc_addr( ); /* set text segments chain */ if (inst_begin == 0) { inst_begin = curr_fence; } else *((word *)last_text) = (word)curr_fence; inst_addr = curr_fence; fix_bb(&text_bytes); while ( (count < text_bytes) && ((eof_flag = get_data(inst_addr, 1)) == 0) ) { current_opcode = *(inst_addr++); (*load_routine[parse_table[current_opcode]])(); count += inst_length[parse_table[current_opcode]]; } if (count != text_bytes) return ( 9 ); /* missing instructions */ return(0); } /* load_text */ load_index() { long psc_offset, clause_no, temp_len; long count = 0; pw psc; pb gen_index(); fix_bb(&index_bytes); while ( (count < index_bytes) && (eof_flag == 0) ) { if (eof_flag = get_data(&psc_offset, 1)) return (10); fix_bb(&psc_offset); psc = (pw)(*reloc_table[psc_offset]); if (eof_flag = get_data(&clause_no, 1)) return (1); fix_bb(&clause_no); if (eof_flag = get_index_tab(clause_no, &temp_len)) return (eof_flag); inst_addr = gen_index(clause_no, psc); count += 8 + temp_len; } return (0); } get_index_tab(clause_no, lenptr) long clause_no, *lenptr; { long hashval, size, j; long count = 0; byte type; word val; pb label, reloc_addr(); hptr = (pb)hreg; size = hsize(clause_no); for (j = 0; j < size; j++) { indextab[j].l = 0; indextab[j].link = (pw)&(indextab[j].link); } for (j = 0; j< clause_no; j++) { if (eof_flag = get_data(&type, 1)) return (11); switch (type) { case 'i': if (eof_flag = get_data(&val, 1)) return (12); fix_bb(&val); count += 9; break; case 'l': val = *((pw)untagged(list_str)); /* val = untagged(list_str); */ count += 5; break; case 'n': /* val = untagged(nil_sym); */ val = *((pw)untagged(nil_sym)); count += 5; break; case 's': if (eof_flag = get_data(&val, 1)) return (12); fix_bb(&val); count += 9; val = *reloc_table[val]; /* val = (word)reloc_table[val]; */ break; case 'c': if (eof_flag = get_data(&val, 1)) return (12); fix_bb(&val); count += 9; val = *reloc_table[val]; /* val = (word)reloc_table[val]; */ break; } if (eof_flag = get_data(&label, 1)) return (13); fix_bb(&label); label = reloc_addr((long)label); hashval = ihash(val, size); inserth(label, &indextab[hashval]); } *lenptr = count; return (0); } byte *gen_index(clause_no, psc) long clause_no; struct psc_rec *psc; { pb ep1, ep2; long j, size; pw temp; size = hsize(clause_no); ep1 = inst_addr; *(ep1++) = hash; *(ep1++) = size; ep2 = (inst_addr + 2 + 4 * size); temp = (pw)(psc->ep + 2); /* here the hash table size is */ *(temp++) = (long)inst_addr + 2; /* computed and inserted into */ *(temp) = size; /* sob instructions */ for (j = 0; j < size; j++) { if (indextab[j].l == 0) { *((pb *)ep1) = trap_vector[0]; ep1 += 4; } else if (indextab[j].l == 1) { *((pw)ep1) = *(indextab[j].link); ep1 += 4; } else { /* otherwise create try/retry/trust instruction */ *((pb *)ep1) = ep2; ep1 += 4; temp = (indextab[j].link); gentry(try, psc->arity, *temp++, ep2); while (*temp != (word)temp) { temp = (pw)(*temp); gentry(retry, psc->arity, *temp++, ep2); } *(ep2 - 6) = trust; } } return (ep2); } inserth(label, bucket) byte *label; struct hrec *bucket; { pw temp; bucket->l++; temp = (pw)&(bucket->link); if (bucket->l > 1) { temp = (pw)*temp; while ((pw)*temp != temp) temp = (pw)*(++temp); } *temp = (word)hptr; *((pb *)hptr) = label; hptr +=4; *((pb *)hptr) = hptr; hptr += 4; } int hsize(numentry) long numentry; { int i, j, temp; temp = numentry + 1; hashsod: j = temp / 2 + 1; for (i = 2; i <= j; i++) { if ((i != temp) && ((temp % i) == 0)) { temp++; goto hashsod;} } return ( temp ); } /************************************************************************ * * * Reloc_addr calculates the entry point of the code using the entry * * point stored in the byte code file as an offset, and the stack * * pointer curr_fence as the relative address. Note trap vectors are * * are indicated with a - 1, in the byte code file. * * * ************************************************************************/ byte *reloc_addr(offset) long offset; { if ( offset >= 0 ) { return curr_fence + offset; } else { if ( -(offset+1) <= maxtraps ) { return trap_vector[-(offset+1)]; } else return curr_fence + offset; /* ??? */ } } /* reloc_addr */ l_E() { } l_B() { if ( get_data(inst_addr,1) ) quit("incomplete instruction\n"); else inst_addr++; } l_PBB() { /* operand 1 = 1 byte; operand 2 = 1 byte */ if ( get_data(inst_addr, 3) ) quit("incomplete instruction\n"); else inst_addr += 3; } l_PW() { /* operand 1 = 4 bytes index */ if ( get_data(inst_addr,5) ) quit("incomplete instruction\n"); else { inst_addr++; fix_bb(inst_addr); st_ptrpsc(inst_addr); inst_addr += 4; } } l_PC() { /* operand 1 = 4 bytes index */ if ( get_data(inst_addr,5) ) quit("incomplete instruction\n"); else { inst_addr++; fix_bb(inst_addr); st_ptrptrpsc(inst_addr); inst_addr += 4; } } l_BW() { /* operand 1 = 4 bytes index; operand 2 = 1 byte */ if ( get_data(inst_addr,5) ) quit("incomplete instruction\n"); else { inst_addr++; fix_bb(inst_addr); st_ptrpsc(inst_addr); inst_addr += 4; } } l_BC() { /* operand 1 = 4 bytes index; operand 2 = 1 byte */ if ( get_data(inst_addr,5) ) quit("incomplete instruction\n"); else { inst_addr++; fix_bb(inst_addr); st_ptrptrpsc(inst_addr); inst_addr += 4; } } l_PA() { /* operand 1 = 4 bytes address */ if ( get_data(inst_addr, 5) ) quit("incomplete instruction\n"); else { inst_addr++; fix_bb(inst_addr); *(pb *)inst_addr = reloc_addr(*(pw)inst_addr); inst_addr += 4; } } l_PL() { /* operand 1 = 4 bytes number */ if ( get_data(inst_addr, 5) ) quit("incomplete instruction\n"); else { inst_addr++; fix_bb(inst_addr); inst_addr += 4; } } l_BL() { /* operand 1 = 4 bytes number; operand 2 = 1 byte reg */ if ( get_data(inst_addr, 5) ) quit("incomplete instruction\n"); else { inst_addr++; fix_bb(inst_addr); inst_addr += 4; } } l_BA() { /* operand 1 = 1 byte reg; operand 2 = 4 bytes address */ if ( get_data(inst_addr, 1) ) quit("incomplete instruction\n"); else { inst_addr++; if ( get_data(inst_addr, 4) ) quit("incomplete instruction\n"); else { fix_bb(inst_addr); *(pb *)inst_addr = reloc_addr(*(pw)inst_addr); inst_addr += 4; } } } l_BAA() { /* oprnd1= 1 byte reg; oprnd1, oprnd2= 4 bytes addr */ if ( get_data(inst_addr, 1) ) quit("incomplete instruction\n"); else { inst_addr++; if ( get_data(inst_addr, 4) ) quit("incomplete instruction\n"); else { fix_bb(inst_addr); *(pb *)inst_addr = reloc_addr(*(pw)inst_addr); inst_addr += 4; if ( get_data(inst_addr, 4) ) quit("incomplete instruction\n"); else { fix_bb(inst_addr); *(pb *)inst_addr = reloc_addr(*(pw)inst_addr); inst_addr += 4; } } } } l_P() { /* only a pad byte, no operand */ if ( get_data(inst_addr, 1) ) quit("incomplete instruction\n"); else inst_addr++; } l_BBB() { /* operand 1 = 1 byte; operand 2 = 1 byte; operand 3 = 1 byte */ if ( get_data(inst_addr, 3) ) quit("incomplete instruction\n"); else inst_addr += 3; } init_load_routine() { load_routine[E] = l_E; load_routine[PBB] = l_PBB; load_routine[BW] = l_BW; load_routine[BC] = l_BC; load_routine[B] = l_B; load_routine[PW] = l_PW; load_routine[PC] = l_PC; load_routine[PL] = l_PL; load_routine[BA] = l_BA; load_routine[BAA] = l_BAA; load_routine[PA] = l_PA; load_routine[BL] = l_BL; load_routine[P] = l_P; load_routine[BBB] = l_BBB; } dyn_loader(psc_ptr) struct psc_rec *psc_ptr; { extern char *getenv(); char s[256], s1[36], *s2, s3[256]; int i; namestring(psc_ptr, s1); if (*s1 == '/') return loader(s1); else if (*s1 == '.') return loader(s1); else { printf("using dynamic loader! %s\n", s1); s2 = getenv("SIMPATH"); while (1) { while (*s2 == ':' || *s2 == ' ') s2++; i = 0; if (*s2 == '\0') { /* file not found */ return 1; } while (*s2 && *s2 != ' ' && *s2 != ':') s[i++] = *(s2++); s[i++] = '/'; s[i] = '\0'; scat(s, s1, s3); if (loader(s3) == 0) return 0; } } }