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
|
|
|
}
|
|
|
|
|
2011-02-14 13:39:43 +00:00
|
|
|
- (void) addInstruction: (Instruction *) inst
|
2005-05-01 11:48:36 +00:00
|
|
|
{
|
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) {
|
2010-12-12 10:40:24 +00:00
|
|
|
[instructions addObject: inst];
|
2005-05-01 11:48:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-14 13:39:43 +00:00
|
|
|
- (integer) addConstant: (SchemeObject *) c
|
2005-05-01 11:48:36 +00:00
|
|
|
{
|
|
|
|
local integer number = [constants count];
|
2010-12-12 10:40:24 +00:00
|
|
|
[constants addObject: c];
|
2005-05-01 11:48:36 +00:00
|
|
|
return number;
|
2005-05-06 23:25:06 +00:00
|
|
|
}
|
2005-05-01 11:48:36 +00:00
|
|
|
|
|
|
|
- (void) compile
|
|
|
|
{
|
|
|
|
local integer 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];
|
2005-05-01 11:48:36 +00:00
|
|
|
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++) {
|
2011-02-14 13:39:43 +00:00
|
|
|
[literals set: index to: (SchemeObject*) [constants objectAtIndex: index]];
|
2005-05-01 11:48:36 +00:00
|
|
|
}
|
|
|
|
for (index = 0; index < [instructions count]; index++) {
|
2010-12-12 01:27:51 +00:00
|
|
|
inst = [instructions objectAtIndex: index];
|
2005-05-01 11:48:36 +00:00
|
|
|
[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
|
|
|
}
|
2005-05-08 10:37:57 +00:00
|
|
|
size = [instructions count];
|
2005-05-01 11:48:36 +00:00
|
|
|
[instructions release];
|
|
|
|
[constants release];
|
2011-01-14 03:07:40 +00:00
|
|
|
instructions = constants = nil;
|
2005-05-01 11:48:36 +00:00
|
|
|
}
|
|
|
|
|
2011-02-14 13:39:43 +00:00
|
|
|
- (instruction_t *) code
|
2005-05-01 11:48:36 +00:00
|
|
|
{
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
2011-02-14 13:39:43 +00:00
|
|
|
- (lineinfo_t *) lineinfo
|
2005-05-08 06:38:01 +00:00
|
|
|
{
|
|
|
|
return lineinfo;
|
|
|
|
}
|
|
|
|
|
2011-02-14 13:39:43 +00:00
|
|
|
- (Frame*) literals
|
2005-05-01 11:48:36 +00:00
|
|
|
{
|
|
|
|
return literals;
|
|
|
|
}
|
|
|
|
|
2005-05-02 02:33:44 +00:00
|
|
|
- (void) dealloc
|
|
|
|
{
|
2011-02-14 13:39:43 +00:00
|
|
|
local Array *temp;
|
2005-05-06 23:25:06 +00:00
|
|
|
|
|
|
|
if (instructions) {
|
|
|
|
temp = instructions;
|
2011-01-14 03:07:40 +00:00
|
|
|
instructions = nil;
|
2005-05-06 23:25:06 +00:00
|
|
|
[temp release];
|
|
|
|
}
|
|
|
|
if (constants) {
|
|
|
|
temp = constants;
|
2011-01-14 03:07:40 +00:00
|
|
|
constants = nil;
|
2005-05-06 23:25:06 +00:00
|
|
|
[temp release];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (code) {
|
2005-05-02 02:33:44 +00:00
|
|
|
obj_free (code);
|
2005-05-06 23:25:06 +00:00
|
|
|
}
|
2005-05-08 10:37:57 +00:00
|
|
|
|
|
|
|
if (lineinfo) {
|
|
|
|
local integer i;
|
|
|
|
for (i = 0; i < size; i++) {
|
|
|
|
[lineinfo[i].sourcefile release];
|
|
|
|
}
|
|
|
|
obj_free (lineinfo);
|
|
|
|
}
|
2005-05-06 23:25:06 +00:00
|
|
|
[super dealloc];
|
2005-05-02 02:33:44 +00:00
|
|
|
}
|
|
|
|
|
2005-05-08 10:37:57 +00:00
|
|
|
- (integer) minimumArguments
|
|
|
|
{
|
|
|
|
return minargs;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) minimumArguments: (integer) min
|
|
|
|
{
|
|
|
|
minargs = min;
|
|
|
|
}
|
|
|
|
|
2005-05-01 11:48:36 +00:00
|
|
|
@end
|