forked from wasm3/wasm3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm3_code.c
145 lines (106 loc) · 3.38 KB
/
m3_code.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//
// m3_code.c
//
// Created by Steven Massey on 4/19/19.
// Copyright © 2019 Steven Massey. All rights reserved.
//
#include "m3_code.h"
IM3CodePage NewCodePage (u32 i_minNumLines)
{
static u32 s_sequence = 0;
IM3CodePage page;
u32 pageSize = sizeof (M3CodePageHeader) + sizeof (code_t) * i_minNumLines;
pageSize = (pageSize + (d_m3CodePageAlignSize-1)) & ~(d_m3CodePageAlignSize-1); // align
m3Alloc ((void **) & page, u8, pageSize);
if (page)
{
page->info.sequence = ++s_sequence;
page->info.numLines = (pageSize - sizeof (M3CodePageHeader)) / sizeof (code_t);
m3log (runtime, "new page: %p; seq: %d; bytes: %d; lines: %d", GetPagePC (page), page->info.sequence, pageSize, page->info.numLines);
}
return page;
}
void FreeCodePages (IM3CodePage * io_list)
{
IM3CodePage page = * io_list;
while (page)
{
m3log (code, "free page: %d; %p; util: %3.1f%%", page->info.sequence, page, 100. * page->info.lineIndex / page->info.numLines);
IM3CodePage next = page->info.next;
m3Free (page);
page = next;
}
* io_list = NULL;
}
u32 NumFreeLines (IM3CodePage i_page)
{
d_m3Assert (i_page->info.lineIndex <= i_page->info.numLines);
return i_page->info.numLines - i_page->info.lineIndex;
}
void EmitWord_impl (IM3CodePage i_page, void * i_word)
{ d_m3Assert (i_page->info.lineIndex+1 <= i_page->info.numLines);
i_page->code [i_page->info.lineIndex++] = i_word;
}
void EmitWord32 (IM3CodePage i_page, const u32 i_word)
{ d_m3Assert (i_page->info.lineIndex+1 <= i_page->info.numLines);
* ((u32 *) & i_page->code [i_page->info.lineIndex++]) = i_word;
}
void EmitWord64 (IM3CodePage i_page, const u64 i_word)
{
#if M3_SIZEOF_PTR == 4
d_m3Assert (i_page->info.lineIndex+2 <= i_page->info.numLines);
* ((u64 *) & i_page->code [i_page->info.lineIndex]) = i_word;
i_page->info.lineIndex += 2;
#else
d_m3Assert (i_page->info.lineIndex+1 <= i_page->info.numLines);
* ((u64 *) & i_page->code [i_page->info.lineIndex]) = i_word;
i_page->info.lineIndex += 1;
#endif
}
pc_t GetPageStartPC (IM3CodePage i_page)
{
return & i_page->code [0];
}
pc_t GetPagePC (IM3CodePage i_page)
{
if (i_page)
return & i_page->code [i_page->info.lineIndex];
else
return NULL;
}
void PushCodePage (IM3CodePage * i_list, IM3CodePage i_codePage)
{
IM3CodePage next = * i_list;
i_codePage->info.next = next;
* i_list = i_codePage;
}
IM3CodePage PopCodePage (IM3CodePage * i_list)
{
IM3CodePage page = * i_list;
* i_list = page->info.next;
page->info.next = NULL;
return page;
}
u32 FindCodePageEnd (IM3CodePage i_list, IM3CodePage * o_end)
{
u32 numPages = 0;
* o_end = NULL;
while (i_list)
{
* o_end = i_list;
++numPages;
i_list = i_list->info.next;
}
return numPages;
}
u32 CountCodePages (IM3CodePage i_list)
{
IM3CodePage unused;
return FindCodePageEnd (i_list, & unused);
}
IM3CodePage GetEndCodePage (IM3CodePage i_list)
{
IM3CodePage end;
FindCodePageEnd (i_list, & end);
return end;
}