static Integer get_index_tab(CTXTdeclc FILE *fd, int clause_no) { Integer hashval, size, j; Integer count = 0; byte type ; CPtr label; Integer ival; Cell val; Integer dummy; /* used to squash warnings */ size = hsize(clause_no); indextab = (struct hrec *)mem_alloc(size*sizeof(struct hrec),COMPILED_SPACE); for (j = 0; j < size; j++) { indextab[j].l = 0; indextab[j].link = (CPtr)&(indextab[j].link); } for (j = 0; j < clause_no; j++) { dummy = get_obj_byte(&type); switch (type) { case 'i': get_obj_word_bbsig_notag(&ival); hashval = ihash((Cell) ival, size); count += 9; break; case 'f': get_obj_word_bbsig_notag(&ival); // printf("sfloat: %f, %x\n",(*(float *)(&ival)), (*(Integer *)(&ival)) ); #ifndef FAST_FLOATS val = float_val_to_hash(*(float *)(&ival)); #else val = ival; #endif hashval = ihash((Cell) val, size); count += 9; break; case 'd': { double fval; dummy = get_obj_string(&fval,8); #ifndef FAST_FLOATS val = float_val_to_hash(fval); #else { union { long intp; float fltp; } cvtr; cvtr.fltp = (float)fval; val = cvtr.intp; } #endif // printf("bld float index: %2.14f, %0x, size=%d\n",fval,val,size); hashval = ihash((Cell) val, size); count += 9; break; } case 'l': hashval = ihash((Cell)(list_pscPair), size); count += 5; break; case 'n': hashval = ihash((Cell) 0, size); count += 5; break; case 'c': get_obj_word_bb(&ival); count += 9; val = (Cell)ival ; st_pscname(&val); hashval = ihash(val, size) ; break; case 's': get_obj_word_bb(&ival); count += 9; val = (Cell)ival ; st_ptrpsc(&val); hashval = ihash(val, size) ; break; default: hashval = 0; xsb_exit( "illegal format"); } get_obj_word_bbsig_notag(&label); label = reloc_addr((Integer)label, seg_text(current_seg)); inserth(label, &indextab[hashval]); } return count; }
off_t Binary::dump(off_t entry) { int size = this->length(); off_t header_offset = 0; /* Reallocate all address if need */ for (int idx = 0; idx < _inst_.size(); ++idx) { int pos; off_t offset = 0; std::stringstream ss; if (!_inst_[idx]->readdress()) { continue; } for(pos =idx+1; pos< _inst_.size(); ++pos) { if ("" != _inst_[pos]->label && _inst_[pos]->label == _inst_[idx]->label) { break; } } if (pos == _inst_.size()) { for(pos =idx-1; pos >= 0; --pos) { if ("" != _inst_[pos]->label && _inst_[pos]->label == _inst_[idx]->label) { break; } } } if (pos == _inst_.size() || pos == idx || 0 > pos) { _E("Not found the symbol %s", _inst_[idx]->label.c_str()); } else if (pos > idx) { for (int i = idx+1; i < pos; ++i) offset += _inst_[i]->length(); } else { for (int i = idx; i > pos; --i) offset -= _inst_[i]->length(); } ss << offset; _inst_[idx]->addIMM(ss.str(), 4, true); } _bin_.open(_src_, std::fstream::out | std::fstream::binary | std::fstream::trunc); /* Create necessary header. NOTE - need run 2 times*/ for (int i=0; i<2; ++i) { header(_bin_, 6, header_offset); seg_pagezero(_bin_); seg_text(_bin_, size, entry, header_offset); seg_linkedit(_bin_); dyld_info(_bin_); dyld_link(_bin_); seg_unixthread(_bin_, entry, header_offset); header_offset = _bin_.tellg(); } /* Write machine code */ for (int idx = 0; idx < _inst_.size(); ++idx) { (*_inst_[idx]) << _bin_; } /* HACK - Mach-O 64 always need large than 4K */ while (0x1000 > _bin_.tellg()) { _bin_.write("\x00", 1); } _bin_.close(); chmod(_src_.c_str(), 0755); return (off_t)0; }