118 lines
1.7 KiB
C
118 lines
1.7 KiB
C
/* Copyright (c) 2024 Freya Murphy */
|
|
|
|
#ifndef __GEN_H__
|
|
#define __GEN_H__
|
|
|
|
#include <mlimits.h>
|
|
#include <mips32.h>
|
|
#include <stdint.h>
|
|
|
|
#include "parse.h"
|
|
#include "tab.h"
|
|
|
|
// predefine
|
|
struct generator;
|
|
|
|
///
|
|
/// a section
|
|
///
|
|
struct section {
|
|
// name
|
|
struct string name;
|
|
|
|
// alignment
|
|
size_t align;
|
|
|
|
// data
|
|
char *data;
|
|
size_t len;
|
|
size_t size;
|
|
|
|
// permissions
|
|
bool read;
|
|
bool write;
|
|
bool execute;
|
|
|
|
/// reference table
|
|
struct reference_table reftab;
|
|
};
|
|
|
|
void section_free(struct section *section);
|
|
|
|
///
|
|
/// instruction generation state
|
|
///
|
|
|
|
struct gen_ins_state {
|
|
// rd,rst,rt
|
|
enum mips32_register rd;
|
|
enum mips32_register rs;
|
|
enum mips32_register rt;
|
|
|
|
// immd
|
|
uint16_t immd;
|
|
|
|
// offset(base)
|
|
uint16_t offset;
|
|
enum mips32_register base;
|
|
|
|
// target
|
|
uint32_t target;
|
|
|
|
// current referencd label
|
|
struct string *label;
|
|
};
|
|
|
|
///
|
|
/// grammer type
|
|
///
|
|
|
|
enum grammer_type {
|
|
GMR_RD,
|
|
GMR_RS,
|
|
GMR_RT,
|
|
GMR_IMMD,
|
|
GMR_OFFSET,
|
|
GMR_OFFSET_BASE,
|
|
GMR_TARGET,
|
|
GMR_HI,
|
|
GMR_LO,
|
|
};
|
|
|
|
///
|
|
/// generates assembley
|
|
/// from a parser stream
|
|
///
|
|
struct generator {
|
|
struct parser parser;
|
|
|
|
// current instruction table
|
|
size_t instructions_len;
|
|
union mips32_instruction *instructions;
|
|
|
|
// current grammer table
|
|
size_t grammers_len;
|
|
struct mips32_grammer *grammers;
|
|
|
|
// segments
|
|
size_t sections_len;
|
|
size_t sections_size;
|
|
struct section *sections;
|
|
|
|
// current section
|
|
struct section *current;
|
|
|
|
// symbol table
|
|
struct symbol_table symtab;
|
|
};
|
|
|
|
/* generate the input as mips32r6 */
|
|
int generate_mips32r6(struct generator *gen);
|
|
|
|
/* initalize a generator */
|
|
int generator_init(const char *file, struct generator *gen);
|
|
|
|
/* free a generator */
|
|
void generator_free(struct generator *gen);
|
|
|
|
#endif /* __GEN_H__ */
|