OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_42/] [or1ksim/] [cpu/] [common/] [parse.c] - Diff between revs 6 and 18

Go to most recent revision | Show entire file | Details | Blame | View Log

Rev 6 Rev 18
Line 22... Line 22...
#include <string.h>
#include <string.h>
#include <stdlib.h>
#include <stdlib.h>
 
 
#include "parse.h"
#include "parse.h"
#include "abstract.h"
#include "abstract.h"
 
#include "arch.h"
 
 
#define MAXLINE_LEN     18000
#define MAXLINE_LEN     18000
 
 
/* Struct labelref that is used in linked list for fixing label refs
 
   after program is loaded to memory. */
 
 
 
struct st_labelref {
 
        struct st_labelref *next;
 
        unsigned long addr;
 
        char label[100];
 
} *labelref = NULL;
 
 
 
/* Unused mem memory marker. It is used when allocating program and data memory
/* Unused mem memory marker. It is used when allocating program and data memory
   during parsing */
   during parsing */
unsigned int freemem;
unsigned int freemem;
 
 
int nonempty(char *line)
int nonempty(char *line)
Line 104... Line 96...
                        }
                        }
                else
                else
                        mem[freemem].data = *str;
                        mem[freemem].data = *str;
}
}
 
 
int is_str(char *str)
 
{
 
        char *c;
 
 
 
        for (c = str; *c != '\0'; c++)
 
                if (isalpha(*c))
 
                        return 1;
 
 
 
        return 0;
 
}
 
 
 
void handle_labelref(struct st_labelref *root)
 
{
 
        unsigned long label;
 
        struct st_labelref *tmp;
 
 
 
        while (root) {
 
                label = eval_label(root->label);
 
                mem[root->addr].data = (char) (label >> 24);
 
                mem[root->addr + 1].data = (char) (label >> 16);
 
                mem[root->addr + 2].data = (char) (label >> 8);
 
                mem[root->addr + 3].data = (char) (label);
 
 
 
                tmp = root->next;
 
                free(root);
 
                root = tmp;
 
        }
 
}
 
 
 
void adddataword(char *num)
void adddataword(char *num)
{
{
 
 
        if (is_str(num)) {
 
                struct st_labelref *new = malloc(sizeof(struct st_labelref));
 
                strcpy(new->label, num);
 
                new->addr = freemem;
 
                new->next = labelref;
 
                labelref = new;
 
        } else {
 
                mem[freemem].data = (char) (atol(num) >> 24);
                mem[freemem].data = (char) (atol(num) >> 24);
                mem[freemem + 1].data = (char) (atol(num) >> 16);
                mem[freemem + 1].data = (char) (atol(num) >> 16);
                mem[freemem + 2].data = (char) (atol(num) >> 8);
                mem[freemem + 2].data = (char) (atol(num) >> 8);
                mem[freemem + 3].data = (char) (atol(num));
                mem[freemem + 3].data = (char) (atol(num));
        }
 
 
 
        freemem += 4;
        freemem += 4;
}
}
 
 
void adddatahalf(char *num)
void adddatahalf(char *num)
Line 184... Line 138...
        return;
        return;
}
}
 
 
void addprogram(char *insn, char *operands)
void addprogram(char *insn, char *operands)
{
{
 
#ifdef  __HALF_WORD_INSN__
 
        int h_insn_is_word_flag=0;
 
        char insn_first2_char[3];
 
 
        strcpy(mem[freemem].insn, insn);
        strcpy(mem[freemem].insn, insn);
 
        insn_first2_char[0]=insn[0];
 
        insn_first2_char[1]=insn[1];
 
        insn_first2_char[2]='\0';
 
 
 
        if(strcmp("h.", insn_first2_char) == 0) {
 
                if(strcmp("h.load32u", insn) == 0 ||
 
                   strcmp("h.load16u", insn) == 0 ||
 
                   strcmp("h.load8u", insn) == 0 ||
 
                   strcmp("h.stor32", insn) == 0 ||
 
                   strcmp("h.stor16", insn) == 0 ||
 
                   strcmp("h.stor8", insn) == 0 ||
 
                   strcmp("h.jal", insn) == 0 ||
 
                   strcmp("h.mtsr", insn) == 0 ||
 
                   strcmp("h.mfsr", insn) == 0 ||
 
                   strcmp("h.movi16ze", insn) == 0 ||
 
                   strcmp("h.immhi16u", insn) == 0 ||
 
                   strcmp("h.addi16s", insn) == 0 ||
 
                   strcmp("h.subi16s", insn) == 0 ||
 
                   strcmp("h.xori16", insn) == 0 ||
 
                   strcmp("h.ori16", insn) == 0 ||
 
                   strcmp("h.andi16", insn) == 0
 
                  )
 
                        h_insn_is_word_flag = 2; /* h.xxx insn AND occupy 4 bytes */
 
                else
 
                        h_insn_is_word_flag = 1; /* h.xxx insn AND occupy 2 bytes */
 
        }
 
        else {
 
                        h_insn_is_word_flag = 0; /* not h.xxx insn */
 
        }
 
#else
 
        strcpy(mem[freemem].insn, insn);
 
#endif
 
 
        /* op1 */
        /* op1 */
        if (*operands)
        if (*operands)
                strcpy(mem[freemem].op1, operands);
                strcpy(mem[freemem].op1, operands);
        if (strstr(mem[freemem].op1, OPERAND_DELIM)) {
        if (strstr(mem[freemem].op1, OPERAND_DELIM)) {
                operands = strstr(mem[freemem].op1, OPERAND_DELIM);
                operands = strstr(mem[freemem].op1, OPERAND_DELIM);
                *operands = '\0';
                *operands = '\0';
                operands++;
                operands++;
        } else {
        } else {
 
#ifdef __HALF_WORD_INSN__
 
                freemem += (h_insn_is_word_flag == 1) ? 2 : 4;
 
#else
                freemem += 4;
                freemem += 4;
 
#endif
                return;
                return;
        }
        }
 
 
        /* op2 */
        /* op2 */
        if (*operands)
        if (*operands)
Line 207... Line 200...
        if (strstr(mem[freemem].op2, OPERAND_DELIM)) {
        if (strstr(mem[freemem].op2, OPERAND_DELIM)) {
                operands = strstr(mem[freemem].op2, OPERAND_DELIM);
                operands = strstr(mem[freemem].op2, OPERAND_DELIM);
                *operands = '\0';
                *operands = '\0';
                operands++;
                operands++;
        } else {
        } else {
 
#ifdef __HALF_WORD_INSN__
 
                freemem += (h_insn_is_word_flag == 1) ? 2 : 4;
 
#else
                freemem += 4;
                freemem += 4;
 
#endif
                return;
                return;
        }
        }
 
 
        /* op3 */
        /* op3 */
        if (*operands)
        if (*operands)
Line 219... Line 216...
        if (strstr(mem[freemem].op3, OPERAND_DELIM)) {
        if (strstr(mem[freemem].op3, OPERAND_DELIM)) {
                operands = strstr(mem[freemem].op3, OPERAND_DELIM);
                operands = strstr(mem[freemem].op3, OPERAND_DELIM);
                *operands = '\0';
                *operands = '\0';
                operands++;
                operands++;
        } else {
        } else {
 
#ifdef __HALF_WORD_INSN__
 
                freemem += (h_insn_is_word_flag == 1) ? 2 : 4;
 
#else
                freemem += 4;
                freemem += 4;
 
#endif
                return;
                return;
        }
        }
 
 
        /* op4 */
        /* op4 */
        if (*operands)
        if (*operands)
Line 232... Line 233...
                operands = strstr(mem[freemem].op4, OPERAND_DELIM);
                operands = strstr(mem[freemem].op4, OPERAND_DELIM);
                *operands = '\0';
                *operands = '\0';
                operands++;
                operands++;
        }
        }
 
 
 
#ifdef __HALF_WORD_INSN__
 
                freemem += (h_insn_is_word_flag == 1) ? 2 : 4;
 
#else
        freemem += 4;
        freemem += 4;
 
#endif
        return;
        return;
}
}
 
 
/* Non-architecture dependent parsing: stripping comments, filling
/* Non-architecture dependent parsing: stripping comments, filling
   abstract memory */
   abstract memory */
Line 257... Line 262...
                        break;
                        break;
                } else
                } else
                        i++;
                        i++;
 
 
        /* Get the first item from this line */
        /* Get the first item from this line */
        strtoken(inputline, item, 1);
        strtoken(inputline, item, 1);   /* opcode */
        strtoken(inputline, item2, 2);
        strtoken(inputline, item2, 2);  /* all the remaining one/two/three operands */
 
 
        /* Is this item empty? Nothing to process, so return. */
        /* Is this item empty? Nothing to process, so return. */
        if (strlen(item) == 0)
        if (strlen(item) == 0)
                return;
                return;
 
 
        /* Is this item a label? If yes, add it to the label
        /* Is this item a label? If yes, add it to the label table and return immediately. */
           table and return immediately. */
 
        if (strstr(item, LABELEND_CHAR)) {
        if (strstr(item, LABELEND_CHAR)) {
                addlabel(item);
                addlabel(item);
                return;
                return;
        }
        }
 
 
Line 340... Line 344...
void loadcode(char *filename)
void loadcode(char *filename)
{
{
        freemem = 0;
        freemem = 0;
        memset(mem, 0, sizeof(mem));
        memset(mem, 0, sizeof(mem));
        readfile(filename);
        readfile(filename);
        handle_labelref(labelref);
 
        return;
        return;
}
}
 
 
 No newline at end of file
 No newline at end of file

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.