quakeforge/ruamoko/scheme/CompiledCode.r

120 lines
2.5 KiB
R
Raw Normal View History

#include "CompiledCode.h"
#include "Symbol.h"
#include "defs.h"
@implementation CompiledCode
- (id) init
{
self = [super init];
constants = [Array new];
instructions = [Array new];
return self;
}
- (void) markReachable
{
[literals mark];
if (constants)
[constants makeObjectsPerformSelector: @selector(mark)];
if (instructions)
[instructions makeObjectsPerformSelector: @selector(mark)];
}
2011-02-14 13:39:43 +00:00
- (void) addInstruction: (Instruction *) inst
{
[inst line: [self line]];
[inst source: [self source]];
[inst offset: [instructions count]];
if ([inst opcode] != LABEL) {
2010-12-12 10:40:24 +00:00
[instructions addObject: inst];
}
}
- (int) addConstant: (SchemeObject *) c
{
local int number = [constants count];
2010-12-12 10:40:24 +00:00
[constants addObject: c];
return number;
}
- (void) compile
{
local int index;
2011-02-14 13:39:43 +00:00
local Instruction *inst;
2011-01-14 03:07:40 +00:00
literals = [Frame newWithSize: [constants count] link: nil];
code = obj_malloc (@sizeof(instruction_t) * [instructions count]);
lineinfo = obj_malloc(@sizeof(lineinfo_t) * [instructions count]);
for (index = 0; index < [constants count]; index++) {
2011-02-14 13:39:43 +00:00
[literals set: index to: (SchemeObject*) [constants objectAtIndex: index]];
}
for (index = 0; index < [instructions count]; index++) {
2010-12-12 01:27:51 +00:00
inst = [instructions objectAtIndex: index];
[inst emitStruct: code];
lineinfo[index].linenumber = [inst line];
lineinfo[index].sourcefile = symbol([inst source]);
[lineinfo[index].sourcefile retain];
}
size = [instructions count];
[instructions release];
[constants release];
2011-01-14 03:07:40 +00:00
instructions = constants = nil;
}
2011-02-14 13:39:43 +00:00
- (instruction_t *) code
{
return code;
}
2011-02-14 13:39:43 +00:00
- (lineinfo_t *) lineinfo
{
return lineinfo;
}
2011-02-14 13:39:43 +00:00
- (Frame*) literals
{
return literals;
}
- (void) dealloc
{
2011-02-14 13:39:43 +00:00
local Array *temp;
if (instructions) {
temp = instructions;
2011-01-14 03:07:40 +00:00
instructions = nil;
[temp release];
}
if (constants) {
temp = constants;
2011-01-14 03:07:40 +00:00
constants = nil;
[temp release];
}
if (code) {
obj_free (code);
}
if (lineinfo) {
local int i;
for (i = 0; i < size; i++) {
[lineinfo[i].sourcefile release];
}
obj_free (lineinfo);
}
[super dealloc];
}
- (int) minimumArguments
{
return minargs;
}
- (void) minimumArguments: (int) min
{
minargs = min;
}
@end