4 /* The compiled code heap is structured into blocks. */
7 // header format (bits indexed with least significant as zero):
9 // bits 1-2: type (as a code_block_type)
11 // bits 3-23: code size / 8
12 // bits 24-31: stack frame size / 16
14 // bits 3-end: code size / 8
16 cell owner; /* tagged pointer to word, quotation or f */
17 cell parameters; /* tagged pointer to array or f */
18 cell relocation; /* tagged pointer to byte-array or f */
22 return (header & 1) == 1;
25 code_block_type type() const
27 return (code_block_type)((header >> 1) & 0x3);
30 void set_type(code_block_type type)
32 header = ((header & ~0x7) | (type << 1));
37 return type() == code_block_pic;
40 bool optimized_p() const
42 return type() == code_block_optimized;
51 size = header & 0xFFFFF8;
52 FACTOR_ASSERT(size > 0);
56 cell stack_frame_size() const
61 return (header >> 20) & 0xFF0;
64 cell stack_frame_size_for_address(cell addr) const
66 cell natural_frame_size = stack_frame_size();
67 /* The first instruction in a code block is the prolog safepoint,
68 and a leaf procedure code block will record a frame size of zero.
69 If we're seeing a stack frame in either of these cases, it's a
70 fake "leaf frame" set up by the signal handler. */
71 if (natural_frame_size == 0 || (void*)addr == entry_point())
72 return LEAF_FRAME_SIZE;
74 return natural_frame_size;
77 void set_stack_frame_size(cell frame_size)
79 FACTOR_ASSERT(size() < 0xFFFFFF);
80 FACTOR_ASSERT(!free_p());
81 FACTOR_ASSERT(frame_size % 16 == 0);
82 FACTOR_ASSERT(frame_size <= 0xFF0);
83 header = (header & 0xFFFFFF) | (frame_size << 20);
86 template<typename Fixup> cell size(Fixup fixup) const
91 void *entry_point() const
93 return (void *)(this + 1);
96 /* GC info is stored at the end of the block */
97 gc_info *block_gc_info() const
99 return (gc_info *)((u8 *)this + size() - sizeof(gc_info));
104 factor::flush_icache((cell)this,size());
107 template<typename Iterator> void each_instruction_operand(Iterator &iter)
109 if(to_boolean(relocation))
111 byte_array *rels = (byte_array *)UNTAG(relocation);
114 cell length = (rels->capacity >> TAG_BITS) / sizeof(relocation_entry);
116 for(cell i = 0; i < length; i++)
118 relocation_entry rel = rels->data<relocation_entry>()[i];
119 iter(instruction_operand(rel,this,index));
120 index += rel.number_of_parameters();
125 cell offset(void *addr) const
127 return (char*)addr - (char*)entry_point();
130 void *address_for_offset(cell offset) const
132 return (void*)((char*)entry_point() + offset);
135 cell scan(factor_vm *vm, void *addr) const;
136 cell owner_quot() const;
139 VM_C_API void undefined_symbol(void);
141 inline code_block *word::code() const {
142 FACTOR_ASSERT(entry_point != NULL);
143 return (code_block*)entry_point - 1;
146 inline code_block *quotation::code() const {
147 FACTOR_ASSERT(entry_point != NULL);
148 return (code_block*)entry_point - 1;