mips/masm/gen.h

168 lines
2.5 KiB
C

/* Copyright (c) 2024 Freya Murphy */
#ifndef __GEN_H__
#define __GEN_H__
#include <mlimits.h>
#include <mips.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,rs,rt
enum mips32_register rd;
enum mips32_register rs;
enum mips32_register rt;
// fs,ft
enum mips32_fp_register fs;
enum mips32_fp_register ft;
enum mips32_fp_register fd;
// immd
uint16_t immd; // 16 bit
// cc
uint16_t cc; // 3 bit
// code
uint32_t code; // 5 bit
// pos
uint32_t pos; // 5 bit
// size
uint32_t size; // 5 bit - 1
// hb
bool hb; // 1 bit - 1
// index
uint32_t index;
// hint
uint32_t hint;
// offset(base)
uint16_t offset; // 16 bit
enum mips32_register base;
// target
uint32_t target;
// current referencd label
struct string *label;
};
struct gen_ins_override {
enum mips32_register reg;
enum mips32_fp_register fpreg;
uint32_t immd;
};
///
/// grammer type
///
enum grammer_type {
// registers
GMR_RD,
GMR_RS,
GMR_RT,
GMR_INDEX_BASE,
// fp registers
GMR_FS,
GMR_FT,
GMR_FD,
// numeric fields
GMR_IMMD,
GMR_CC,
GMR_CODE,
GMR_POS,
GMR_SIZE,
GMR_HB,
GMR_HINT,
// addresses
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);
/* run codegen with the mips32r2 specification */
int generate_mips32r2(struct generator *gen);
/* run codegen with the mips32r6 specification */
int generate_mips1(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__ */