/
loader.c
152 lines (121 loc) · 3.96 KB
/
loader.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
146
147
148
149
150
151
152
// This will load a lorito bytecode file into a lorito codeseg
// Since this is temporary, and we'll end up throwing it away in favor of
// integrating with parrot's packfile format, this will be real simple.
//
// Integer: segment type (0 = code, 1 = const, 2 = datadef)
// Integer: Size of segement name
// String: segment name, null terminated
// Integer: Count (in 8 bytes, so a count of 1 == 8 bytes)
// Data
//
// The datadef segment has no data. It is equivalent to a bss in asm terms.
#include "lorito.h"
#include "internal_pmc.h"
#include "loader.h"
#include <stdio.h>
void file_info(Lorito_Interp *interp)
{
int i, j;
for (i = 0; i < interp->next_fileid; i++)
{
Lorito_File *file = interp->files[i];
//printf("%s\n", file->name);
for (j = 0; j < file->constseg_count; j++)
{
Lorito_Constseg *constseg = file->constsegs[j];
//printf(" DSegNum: %d\n", constseg->segid);
//printf(" DSegName: %s\n", constseg->name);
}
for (j = 0; j < file->codeseg_count; j++)
{
Lorito_Codeseg *codeseg = file->codesegs[j];
//printf(" CSegNum: %d\n", codeseg->segid);
//printf(" CSegName: %s\n", codeseg->name);
}
}
}
void
loadbc(Lorito_Interp *interp, char* filename)
{
int fileid = interp->next_fileid++;
int segid = 0;
FILE *input = fopen(filename, "rb");
if (input == NULL) {
perror("Failed to open file");
return;
}
if (fileid >= interp->allocated_file)
{
interp->allocated_file += 4;
interp->files = realloc(interp->files, interp->allocated_file);
if (interp->files == NULL)
abort();
}
/*
Lorito_File *file = &interp->files[fileid];
lorito_pmc_init(interp, (Lorito_PMC *) file);
*/
Lorito_File *file = lorito_file_new(interp, filename);
interp->files[fileid] = file;
file->fileid = fileid;
while (!feof(input))
{
int read = 0;
int typed = 0;
read = fread(&typed, sizeof(int), 1, input);
if (read == 0)
break;
// or die
if (typed == SEG_code)
{
int length = 0;
read = fread(&length, sizeof(int), 1, input);
// or die
char *name = (char *) malloc(sizeof(char) * length);
read = fread(name, sizeof(char), length, input);
// or die
read = fread(&length, sizeof(int), 1, input);
// or die
Lorito_Opcode *op = (Lorito_Opcode *) malloc(sizeof(Lorito_Opcode) * length);
read = fread(op, sizeof(Lorito_Opcode), length, input);
// or die
file->codesegs = realloc(file->codesegs, (file->codeseg_count+1) * sizeof(Lorito_Constseg *));
if (interp->files == NULL)
abort();
Lorito_Codeseg *codeseg = lorito_code_block_new(interp, name, length, op);
file->codesegs[file->codeseg_count] = codeseg;
file->codeseg_count++;
codeseg->fileid = fileid;
codeseg->file = file;
codeseg->segid = segid++;
interp->last_seg = codeseg;
//printf("SegNum: %d\n", codeseg->segid);
//printf("SegName: %s\n", codeseg->name);
}
if (typed == SEG_const)
{
int length = 0;
read = fread(&length, sizeof(int), 1, input);
// or die
char *name = (char *) malloc(sizeof(char) * length);
read = fread(name, sizeof(char), length, input);
// or die
read = fread(&length, sizeof(int), 1, input);
// or die
void *data = (void *) malloc(sizeof(Lorito_Opcode) * length);
read = fread(data, sizeof(Lorito_Opcode), length, input);
// or die
file->constsegs = realloc(file->constsegs, (file->constseg_count+1) * sizeof(Lorito_Constseg *));
if (interp->files == NULL)
abort();
Lorito_Constseg *constseg = lorito_const_block_new(interp, name, length, data);
constseg->fileid = fileid;
constseg->file = file;
constseg->segid = segid++;
file->constsegs[file->constseg_count] = constseg;
file->constseg_count++;
//printf("DSegNum: %d\n", constseg->segid);
//printf("DSegName: %s\n", constseg->name);
}
}
}