
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "defines.h"

#define STRING_LINE 512
#define DATA_ADDR_BOUNDARY 6
#define DATA_MAX_LENGTH  10
//#define OUTPUT_FILE_NAME "kernel.asm"
#define INSTRUCTION_SIZE 32
#define ADDRESS_SIZE     32
#define HEX_SIZE         (INSTRUCTION_SIZE/4)
#define MAX_ILLEGAL      32


#define MEMINIT "MEMINIT{"
#define MEMINITFUNC "MEMINITFUNC{for (i=0;i<"
#define IMEM_RELOC "IMEM_RELOC"
#define TRAP "P0 trap\n"
#define InstrRET "P0 jmp.reg R31\n"
#define MAINL "main:\n"
#define PRINTF "printf:\n"

typedef struct 
{
    int start;        //global variables start address (original, depend on LCC)
    int end;          //global variables end address (original, depend on LCC)
    int new_start;    //new start address
    int new_end;      //new end address
    char** variables;
    
    struct gVars* link;
}gVars;

typedef struct
{
    int location;       //specified instruction location
    //int offset;         //relative offset of variable defined by LCC
    //int index;          //relative index in gVars list

    struct instrInfo* link;
}instrInfo;


gVars *bss;              //store all non-initialized global or static data
//gVars *rodata;
gVars *all_data;         //store all initialized global or static data
instrInfo *all_instr;    //store all related data-access instruction info
int task_select;     //which index of task address is selected (1-5 now)
int task_addr;       //task physical address selected by user statically
int taskD_addr;      //task data physical start address
int taskH_addr;      //task heap physical start address
int taskS_addr;      //task stack physical start address
int taskF_addr;      //task frame physical start address

int text_size;
int bss_size;
int data_size;


/* function prototypes */
int HexToDec(char *data, int length);
int eval_num_vars(int start, int end);
int judge_data(char *data);
int get_offset(char *data);
void filldata(char *data, int size, FILE *outFP);
int separate_bss(gVars *data, int data_count);
int rearrangeData();
int adjust(int k);
int parser(char *file_name);
int loader();
int locator(char *file_name);
int generate_data_binary(char *file_name);

