]> gitweb.factorcode.org Git - factor.git/blob - vm/code_blocks.hpp
baf763357c5911f379c928160d708ab10141dd06
[factor.git] / vm / code_blocks.hpp
1 namespace factor
2 {
3
4 /* The compiled code heap is structured into blocks. */
5 struct code_block
6 {
7         cell header;
8         cell owner; /* tagged pointer to word, quotation or f */
9         cell parameters; /* tagged pointer to array or f */
10         cell relocation; /* tagged pointer to byte-array or f */
11
12         bool free_p() const
13         {
14                 return (header & 1) == 1;
15         }
16
17         code_block_type type() const
18         {
19                 return (code_block_type)((header >> 1) & 0x3);
20         }
21
22         void set_type(code_block_type type)
23         {
24                 header = ((header & ~0x7) | (type << 1));
25         }
26
27         bool pic_p() const
28         {
29                 return type() == code_block_pic;
30         }
31
32         bool optimized_p() const
33         {
34                 return type() == code_block_optimized;
35         }
36
37         cell size() const
38         {
39                 cell size = header & ~7;
40 #ifdef FACTOR_DEBUG
41                 assert(size > 0);
42 #endif
43                 return size;
44         }
45
46         void *entry_point() const
47         {
48                 return (void *)(this + 1);
49         }
50
51         void flush_icache()
52         {
53                 factor::flush_icache((cell)this,size());
54         }
55
56         template<typename Iterator> void each_instruction_operand(Iterator &iter)
57         {
58                 if(to_boolean(relocation))
59                 {
60                         byte_array *rels = (byte_array *)UNTAG(relocation);
61
62                         cell index = 0;
63                         cell length = (rels->capacity >> TAG_BITS) / sizeof(relocation_entry);
64
65                         for(cell i = 0; i < length; i++)
66                         {
67                                 relocation_entry rel = rels->data<relocation_entry>()[i];
68                                 iter(instruction_operand(rel,this,index));
69                                 index += rel.number_of_parameters();
70                         }
71                 }
72         }
73 };
74
75 }