Skip to content

Commit

Permalink
Implemented Vanilla Register allocator. PASM emitter is currently under
Browse files Browse the repository at this point in the history
development. 


git-svn-id: https://svn.parrot.org/parrot/trunk@13918 d31e2699-5ff4-0310-a27c-f18f2fbe73fe
  • Loading branch information
vishalrsoni committed Aug 8, 2006
1 parent 08fd404 commit 144253f
Show file tree
Hide file tree
Showing 12 changed files with 314 additions and 58 deletions.
3 changes: 2 additions & 1 deletion compilers/bcg/include/bcg.h
Expand Up @@ -35,12 +35,13 @@ void BCG_end_call(BCG_info * bcg_info);
void BCG_start_op(BCG_info * bcg_info, char *op);
void BCG_end_op(BCG_info * bcg_info);
void BCG_var(BCG_info * bcg_info, char *var_name, char var_type);
void BCG_val(BCG_info * bcg_info, char *val);
void BCG_val(BCG_info * bcg_info, char *val, char val_type);
void BCG_label(BCG_info * bcg_info, char *label);

/* Functions to manipulate the internals of BCG_info structure. */
//void BCG_set_interpreter(BCG_info *bcg_info, Interp *interp);
//char *BCG_get_error_message(BCG_info *bcg_info);
//int BCG_get_error_code(BCG_info *bcg_info);

void BCG_print_pasm(BCG_info *bcg_info);
#endif /* PARROT_BCG_H_GUARD */
62 changes: 43 additions & 19 deletions compilers/bcg/src/bcg.c
Expand Up @@ -23,6 +23,8 @@ List of methods imlemented in this file.
#include "bcg.h"
#include "bcg_private.h"
#include "bcg_logger.h"
#include "bcg_reg_alloc.h"
#include "bcg_emitter.h"

/* Forward decleration */

Expand Down Expand Up @@ -110,17 +112,22 @@ BCG_start_sub(BCG_info * bcg_info, char *sub_name, char *pragma)
set_state(bcg_info, BCG_STATE_IN_SUB);
unit = bcg_unit_create(bcg_info, sub_name, pragma);
bcg_info_add_unit(bcg_info, unit);
unit->symbol_table = bcg_hash_create(bcg_info);
}

void
BCG_end_sub(BCG_info * bcg_info)
{
bcg_unit *curr_unit;

if (!in_state(bcg_info, BCG_STATE_IN_SUB)) {
bcg_throw_exception(bcg_info, BCG_EXCEPTION,
"Expected BCG to be in IN_SUB state.");
}

unset_state(bcg_info, BCG_STATE_IN_SUB);
curr_unit = bcg_info_current_unit(bcg_info);
reg_alloc_vanilla(bcg_info, curr_unit);
}

void
Expand Down Expand Up @@ -158,24 +165,29 @@ BCG_start_op(BCG_info * bcg_info, char *op_name)

set_state(bcg_info, BCG_STATE_IN_OP);
curr_unit = bcg_info_current_unit(bcg_info);
op = bcg_op_create(bcg_info, op_name);
op = bcg_op_create(bcg_info, op_name, BCG_OP);
bcg_unit_add_op(bcg_info, curr_unit, op);
}

void
BCG_end_op(BCG_info * bcg_info)
{
bcg_op *curr_op;

if (!in_state(bcg_info, BCG_STATE_IN_OP)) {
bcg_throw_exception(bcg_info, BCG_EXCEPTION,
"Expected BCG to be in IN_CALL state.");
}

unset_state(bcg_info, BCG_STATE_IN_OP);
curr_op = bcg_info_current_op(bcg_info);
bcg_op_resolve_full_name(bcg_info, curr_op);
}

void
BCG_var(BCG_info * bcg_info, char *var_name, char var_type)
{
bcg_unit *curr_unit;
bcg_op *curr_op;
bcg_op_arg *op_arg;

Expand All @@ -184,28 +196,54 @@ BCG_var(BCG_info * bcg_info, char *var_name, char var_type)
"Expected BCG to be in IN_OP state.");
}

UNUSED(var_type); /* TODO value the var_type. */
curr_unit = bcg_info_current_unit(bcg_info);
curr_op = bcg_info_current_op(bcg_info);
op_arg = bcg_op_arg_create(bcg_info, var_name, BCG_OP_ARG_TYPE_VAR_PMC);
bcg_add_op_arg(bcg_info, curr_op, op_arg);
op_arg = bcg_hash_get(bcg_info, curr_unit->symbol_table, var_name);
if (op_arg == NULL) {
op_arg =
bcg_op_arg_create(bcg_info, var_name, BCG_OP_ARG_VARIABLE,
var_type);
}
bcg_op_add_arg(bcg_info, curr_op, op_arg);
bcg_hash_put(bcg_info, curr_unit->symbol_table, var_name, op_arg);
}

void
BCG_val(BCG_info * bcg_info, char *val)
BCG_val(BCG_info * bcg_info, char *val, char val_type)
{
bcg_op *curr_op;
bcg_op_arg *op_arg;

if (!in_state(bcg_info, BCG_STATE_IN_OP)) {
bcg_throw_exception(bcg_info, BCG_EXCEPTION,
"Expected BCG to be in IN_OP state.");
}

curr_op = bcg_info_current_op(bcg_info);
op_arg = bcg_op_arg_create(bcg_info, val, BCG_OP_ARG_CONSTANT, val_type);
bcg_op_add_arg(bcg_info, curr_op, op_arg);
}

void
BCG_label(BCG_info * bcg_info, char *label)
{
bcg_unit *curr_unit;
bcg_op *op;

if (!in_state(bcg_info, BCG_STATE_IN_SUB)) {
bcg_throw_exception(bcg_info, BCG_EXCEPTION,
"Expected BCG to be in IN_SUB state.");
}

curr_unit = bcg_info_current_unit(bcg_info);
op = bcg_op_create(bcg_info, label, BCG_OP_LABEL);
bcg_unit_add_op(bcg_info, curr_unit, op);
}

void
BCG_print_pasm(BCG_info * bcg_info)
{
emit_pasm(bcg_info);
}

bcg_info_private *
Expand Down Expand Up @@ -253,20 +291,6 @@ bcg_info_add_unit(BCG_info * bcg_info, bcg_unit * unit)

}

bcg_unit *
bcg_info_current_unit(BCG_info * bcg_info)
{
bcg_info_private *bcg_info_priv = BCG_INFO_PRIV(bcg_info);
return bcg_info_priv->last_unit;
}

bcg_op *
bcg_info_current_op(BCG_info * bcg_info)
{
bcg_info_private *bcg_info_priv = BCG_INFO_PRIV(bcg_info);
return bcg_info_priv->last_unit->last_op;
}

static void
set_state(BCG_info * bcg_info, bcg_state state)
{
Expand Down
9 changes: 9 additions & 0 deletions compilers/bcg/src/bcg_emitter.h
@@ -0,0 +1,9 @@
#ifndef PARROT_BCG_EMTITTER_H_GUARD
#define PARROT_BCG_EMTITTER_H_GUARD

#include "bcg.h"

/* TODO Define standard emitter API that can support multiple emitter*/
void emit_pasm(BCG_info * info);

#endif
43 changes: 43 additions & 0 deletions compilers/bcg/src/bcg_emitter_pasm.c
@@ -0,0 +1,43 @@
#include "bcg_emitter.h"
#include "bcg_private.h"
#include "bcg.h"

void
emit_pasm(BCG_info * bcg_info)
{
bcg_unit *unit;
bcg_info_private *bcg_info_priv;

bcg_info_priv = BCG_INFO_PRIV(bcg_info);
unit = bcg_info_priv->first_unit;

while (unit) {
bcg_op *op;
op = unit->first_op;

printf("_%s:\n", unit->name);
while (op) {
if (op->type == BCG_OP_LABEL) {
printf("%s:\n", op->name);
}
else {
int i;
bcg_op_arg *op_arg;
printf(" %s", op->full_name);
for (i = 0; i < op->op_arg_count; i++) {
op_arg = op->op_args[i];
if (op_arg->is_constant) {
printf(", \"%s\"", op_arg->name);
}
else {
printf(" %c%d", op_arg->data_type, op_arg->reg_num);
}
}
printf("\n");
}

op = op->next;
}
unit = unit->next;
}
}
53 changes: 44 additions & 9 deletions compilers/bcg/src/bcg_op.c
@@ -1,16 +1,21 @@
#include <ctype.h>
#include <string.h>
#include "parrot/parrot.h"
#include "bcg_private.h"
#include "bcg.h"

bcg_op_arg *
bcg_op_arg_create(BCG_info * bcg_info, char *name, bcg_op_arg_type type)
bcg_op_arg_create(BCG_info * bcg_info, char *name, bcg_op_arg_type type,
char data_type)
{
bcg_op_arg *op_arg;

UNUSED(bcg_info);
op_arg = (bcg_op_arg *) mem_sys_allocate_zeroed(sizeof(bcg_op_arg));
op_arg->name = name;
op_arg->type = type;
op_arg->is_constant = type;
op_arg->data_type = data_type;
op_arg->reg_num = -1;
return op_arg;
}

Expand All @@ -23,14 +28,15 @@ bcg_op_arg_destroy(BCG_info * bcg_info, bcg_op_arg * op_arg)
}

bcg_op *
bcg_op_create(BCG_info * bcg_info, char *name)
bcg_op_create(BCG_info * bcg_info, char *name, bcg_op_type op_type)
{
bcg_op *op;

UNUSED(bcg_info);
op = (bcg_op *) mem_sys_allocate_zeroed(sizeof(bcg_op));
op->name = name;
op->op_arg_count = 0;
op->type = op_type;
return op;
}

Expand All @@ -41,19 +47,48 @@ bcg_op_destroy(BCG_info * bcg_info, bcg_op * op)

UNUSED(bcg_info);
mem_sys_free(op->name);
mem_sys_free(op->full_name);
for (i = 0; i < (op->op_arg_count); i++) {
bcg_op_arg_destroy(bcg_info, op->op_args[i]);
}
mem_sys_free(op);
}

void
bcg_add_op_arg(BCG_info * bcg_info, bcg_op * op, bcg_op_arg * op_arg)
bcg_op_add_arg(BCG_info * bcg_info, bcg_op * op, bcg_op_arg * op_arg)
{
UNUSED(bcg_info);
op->op_args[op->op_arg_count]=op_arg;
op->op_arg_count++;
*(op->op_args) =
(bcg_op_arg *) mem_sys_allocate_zeroed(sizeof(bcg_op_arg *) *
op->op_arg_count);
op->op_args[(op->op_arg_count) - 1] = op_arg;
}

void
bcg_op_resolve_full_name(BCG_info * bcg_info, bcg_op * op)
{
char buffer[16];
char *buff_ptr, *full_name;
int len, i;

buff_ptr = buffer;
len = sprintf(buff_ptr, "%s", op->name);
buff_ptr += len;

for (i = 0; i < op->op_arg_count; i++) {
len = sprintf(buff_ptr, "_%c", tolower(op->op_args[i]->data_type));
buff_ptr += len;
if (op->op_args[i]->is_constant) {
len = sprintf(buff_ptr, "c");
buff_ptr += len;
}
}

full_name = (char *)mem_sys_allocate_zeroed(sizeof(char) * strlen(buffer));
strcpy(full_name, buffer);
op->full_name = full_name;
}

bcg_op *
bcg_info_current_op(BCG_info * bcg_info)
{
bcg_info_private *bcg_info_priv = BCG_INFO_PRIV(bcg_info);
return bcg_info_priv->last_unit->last_op;
}
35 changes: 19 additions & 16 deletions compilers/bcg/src/bcg_private.h
Expand Up @@ -15,17 +15,6 @@ typedef enum bcg_state_t {
BCG_STATE_IN_CALL = 1 << 3
} bcg_state;

typedef enum bcg_op_argtype_t {
BCG_OP_ARG_TYPE_CONST_INT = 1,
BCG_OP_ARG_TYPE_CONST_NUM = 2,
BCG_OP_ARG_TYPE_CONST_STR = 3,
BCG_OP_ARG_TYPE_CONST_PMC = 4,
BCG_OP_ARG_TYPE_VAR_INT = 10,
BCG_OP_ARG_TYPE_VAR_NUM = 20,
BCG_OP_ARG_TYPE_VAR_STR = 30,
BCG_OP_ARG_TYPE_VAR_PMC = 40
} bcg_op_arg_type;

typedef enum bcg_unit_pragma_t {
BCG_UNIT_PRAGMA_NONE = 0,
BCG_UNIT_PRAGMA_MAIN = 1,
Expand All @@ -34,17 +23,30 @@ typedef enum bcg_unit_pragma_t {
BCG_UNIT_PRAGMA_INIT = 4
} bcg_unit_pragma;

typedef enum bcg_op_arg_type_t {
BCG_OP_ARG_VARIABLE = 0,
BCG_OP_ARG_CONSTANT = 1
} bcg_op_arg_type;

typedef enum bcg_op_type_t {
BCG_OP = 0,
BCG_OP_LABEL = 1
} bcg_op_type;

typedef struct bcg_op_arg_t {
int type;
int is_constant;
char data_type;
char *name;
int reg_num;
} bcg_op_arg;

typedef struct bcg_op_t {
char *name;
int op_code;
int type;
char *full_name;
int op_arg_count;
bcg_op_arg *op_args[0];
bcg_op_arg *op_args[4]; /* TODO fix this constant. */
struct bcg_op_t *prev;
struct bcg_op_t *next;
} bcg_op;
Expand All @@ -68,13 +70,14 @@ typedef struct bcg_info_private_t {

/* Functions for Op Arguments. */
bcg_op_arg *bcg_op_arg_create(BCG_info * bcg_info, char *name,
bcg_op_arg_type type);
bcg_op_arg_type type, char data_type);
void bcg_op_arg_destroy(BCG_info * bcg_info, bcg_op_arg * op_arg);

/* Functions for Ops. */
bcg_op *bcg_op_create(BCG_info * bcg_info, char *name);
bcg_op *bcg_op_create(BCG_info * bcg_info, char *name, bcg_op_type op_type);
void bcg_op_destroy(BCG_info * bcg_info, bcg_op * op);
void bcg_add_op_arg(BCG_info * bcg_info, bcg_op * op, bcg_op_arg * op_arg);
void bcg_op_add_arg(BCG_info * bcg_info, bcg_op * op, bcg_op_arg * op_arg);
void bcg_op_resolve_full_name(BCG_info * bcg_info, bcg_op * op);

/* Function for Byte Code Generation unit. */
bcg_unit *bcg_unit_create(BCG_info * bcg_info, char *name, char *pragma);
Expand Down
10 changes: 10 additions & 0 deletions compilers/bcg/src/bcg_reg_alloc.h
@@ -0,0 +1,10 @@
#ifndef PARROT_BCG_REG_ALLOC_H_GUARD
#define PARROT_BCG_REG_ALLOC_H_GUARD

#include "bcg.h"
#include "bcg_private.h"

/* TODO standardize register allocation API to support different allocators.*/
void reg_alloc_vanilla(BCG_info * bcg_info, bcg_unit * unit);

#endif /* PARROT_BCG_REG_ALLOC_H_GUARD */

0 comments on commit 144253f

Please sign in to comment.