]> gitweb.factorcode.org Git - factor.git/blob - vm/code_blocks.hpp
vm: pack frame size into code block header bits
[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         // header format (bits indexed with least significant as zero):
8         // bit   0  : free?
9         // bits  1-2: type (as a code_block_type)
10         // if not free:
11         //   bits  3-23: code size / 8
12         //   bits 24-31: stack frame size / 16
13         // if free:
14         //   bits  3-end: code size / 8
15         cell header;
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 */
19
20         bool free_p() const
21         {
22                 return (header & 1) == 1;
23         }
24
25         code_block_type type() const
26         {
27                 return (code_block_type)((header >> 1) & 0x3);
28         }
29
30         void set_type(code_block_type type)
31         {
32                 header = ((header & ~0x7) | (type << 1));
33         }
34
35         bool pic_p() const
36         {
37                 return type() == code_block_pic;
38         }
39
40         bool optimized_p() const
41         {
42                 return type() == code_block_optimized;
43         }
44
45         cell size() const
46         {
47                 cell size;
48                 if (free_p())
49                         size = header & ~7;
50                 else
51                         size = header & 0xFFFFF8;
52                 FACTOR_ASSERT(size > 0);
53                 return size;
54         }
55
56         cell stack_frame_size() const
57         {
58                 if (free_p())
59                         return 0;
60                 else
61                         return (header >> 20) & 0xFF0;
62         }
63
64         void set_stack_frame_size(cell frame_size)
65         {
66                 FACTOR_ASSERT(size() < 0xFFFFFF);
67                 FACTOR_ASSERT(!free_p());
68                 FACTOR_ASSERT(frame_size % 16 == 0);
69                 FACTOR_ASSERT(frame_size <= 0xFF0);
70                 header = (header & 0xFFFFFF) | (frame_size << 20);
71         }
72
73         template<typename Fixup> cell size(Fixup fixup) const
74         {
75                 return size();
76         }
77
78         void *entry_point() const
79         {
80                 return (void *)(this + 1);
81         }
82
83         /* GC info is stored at the end of the block */
84         gc_info *block_gc_info() const
85         {
86                 return (gc_info *)((u8 *)this + size() - sizeof(gc_info));
87         }
88
89         void flush_icache()
90         {
91                 factor::flush_icache((cell)this,size());
92         }
93
94         template<typename Iterator> void each_instruction_operand(Iterator &iter)
95         {
96                 if(to_boolean(relocation))
97                 {
98                         byte_array *rels = (byte_array *)UNTAG(relocation);
99
100                         cell index = 0;
101                         cell length = (rels->capacity >> TAG_BITS) / sizeof(relocation_entry);
102
103                         for(cell i = 0; i < length; i++)
104                         {
105                                 relocation_entry rel = rels->data<relocation_entry>()[i];
106                                 iter(instruction_operand(rel,this,index));
107                                 index += rel.number_of_parameters();
108                         }
109                 }
110         }
111 };
112
113 VM_C_API void undefined_symbol(void);
114
115 inline code_block *word::code() const {
116         FACTOR_ASSERT(entry_point != NULL);
117         return (code_block*)entry_point - 1;
118 }
119
120 inline code_block *quotation::code() const {
121         FACTOR_ASSERT(entry_point != NULL);
122         return (code_block*)entry_point - 1;
123 }
124
125 }
126
127