2005-05-01 11:48:36 +00:00
|
|
|
#include "CompiledCode.h"
|
2005-05-08 06:38:01 +00:00
|
|
|
#include "Symbol.h"
|
2005-05-01 11:48:36 +00:00
|
|
|
#include "defs.h"
|
|
|
|
|
|
|
|
@implementation CompiledCode
|
|
|
|
- (id) init
|
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
constants = [Array new];
|
|
|
|
instructions = [Array new];
|
|
|
|
return self;
|
|
|
|
}
|
2005-05-02 02:33:44 +00:00
|
|
|
|
2005-05-08 06:38:01 +00:00
|
|
|
|
|
|
|
|
2005-05-02 02:33:44 +00:00
|
|
|
- (void) markReachable
|
|
|
|
{
|
|
|
|
[literals mark];
|
2005-05-06 23:25:06 +00:00
|
|
|
if (constants)
|
|
|
|
[constants makeObjectsPerformSelector: @selector(mark)];
|
|
|
|
if (instructions)
|
|
|
|
[instructions makeObjectsPerformSelector: @selector(mark)];
|
2005-05-02 02:33:44 +00:00
|
|
|
}
|
|
|
|
|
2005-05-01 11:48:36 +00:00
|
|
|
- (void) addInstruction: (Instruction) inst
|
|
|
|
{
|
2005-05-08 06:38:01 +00:00
|
|
|
[inst line: [self line]];
|
|
|
|
[inst source: [self source]];
|
2005-05-01 11:48:36 +00:00
|
|
|
[inst offset: [instructions count]];
|
|
|
|
if ([inst opcode] != LABEL) {
|
|
|
|
[instructions addItem: inst];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (integer) addConstant: (SchemeObject) c
|
|
|
|
{
|
|
|
|
local integer number = [constants count];
|
|
|
|
[constants addItem: c];
|
|
|
|
return number;
|
2005-05-06 23:25:06 +00:00
|
|
|
}
|
2005-05-01 11:48:36 +00:00
|
|
|
|
|
|
|
- (void) compile
|
|
|
|
{
|
|
|
|
local integer index;
|
|
|
|
local Instruction inst;
|
|
|
|
literals = [Frame newWithSize: [constants count] link: NIL];
|
|
|
|
code = obj_malloc (@sizeof(instruction_t) * [instructions count]);
|
2005-05-08 06:38:01 +00:00
|
|
|
lineinfo = obj_malloc(@sizeof(lineinfo_t) * [instructions count]);
|
2005-05-01 11:48:36 +00:00
|
|
|
for (index = 0; index < [constants count]; index++) {
|
|
|
|
[literals set: index to: (SchemeObject) [constants getItemAt: index]];
|
|
|
|
}
|
|
|
|
for (index = 0; index < [instructions count]; index++) {
|
|
|
|
inst = [instructions getItemAt: index];
|
|
|
|
[inst emitStruct: code];
|
2005-05-08 06:38:01 +00:00
|
|
|
lineinfo[index].linenumber = [inst line];
|
|
|
|
lineinfo[index].sourcefile = symbol([inst source]);
|
|
|
|
[lineinfo[index].sourcefile retain];
|
2005-05-01 11:48:36 +00:00
|
|
|
}
|
|
|
|
[instructions release];
|
|
|
|
[constants release];
|
2005-05-02 02:33:44 +00:00
|
|
|
instructions = constants = NIL;
|
2005-05-01 11:48:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (instruction_t []) code
|
|
|
|
{
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
2005-05-08 06:38:01 +00:00
|
|
|
- (lineinfo_t []) lineinfo
|
|
|
|
{
|
|
|
|
return lineinfo;
|
|
|
|
}
|
|
|
|
|
2005-05-01 11:48:36 +00:00
|
|
|
- (Frame) literals
|
|
|
|
{
|
|
|
|
return literals;
|
|
|
|
}
|
|
|
|
|
2005-05-02 02:33:44 +00:00
|
|
|
- (void) dealloc
|
|
|
|
{
|
2005-05-06 23:25:06 +00:00
|
|
|
local Array temp;
|
|
|
|
|
|
|
|
if (instructions) {
|
|
|
|
temp = instructions;
|
|
|
|
instructions = NIL;
|
|
|
|
[temp release];
|
|
|
|
}
|
|
|
|
if (constants) {
|
|
|
|
temp = constants;
|
|
|
|
constants = NIL;
|
|
|
|
[temp release];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (code) {
|
2005-05-02 02:33:44 +00:00
|
|
|
obj_free (code);
|
2005-05-06 23:25:06 +00:00
|
|
|
}
|
|
|
|
[super dealloc];
|
2005-05-02 02:33:44 +00:00
|
|
|
}
|
|
|
|
|
2005-05-01 11:48:36 +00:00
|
|
|
@end
|