2016-09-18 21:24:54 +00:00
|
|
|
#include "mcg.h"
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
static struct symbol* pending;
|
|
|
|
|
|
|
|
void data_label(const char* label)
|
|
|
|
{
|
2016-11-27 20:18:00 +00:00
|
|
|
struct symbol* sym = symbol_get(label);
|
|
|
|
if (sym->is_defined)
|
|
|
|
fatal("label '%s' defined twice", sym->name);
|
2016-09-18 21:24:54 +00:00
|
|
|
|
2016-11-27 20:18:00 +00:00
|
|
|
if (pending)
|
|
|
|
fprintf(outputfile, "%s = %s\n",
|
|
|
|
platform_label(label), platform_label(pending->name));
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pending = sym;
|
|
|
|
pending = symbol_get(label);
|
|
|
|
pending->is_defined = true;
|
|
|
|
}
|
2016-09-18 21:24:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char* section_to_str(int section)
|
|
|
|
{
|
|
|
|
switch (section)
|
|
|
|
{
|
|
|
|
case SECTION_ROM: return ".rom";
|
|
|
|
case SECTION_DATA: return ".data";
|
|
|
|
case SECTION_BSS: return ".bss";
|
|
|
|
case SECTION_TEXT: return ".text";
|
|
|
|
default: return "unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void emit_header(int desired_section)
|
|
|
|
{
|
|
|
|
if (pending)
|
|
|
|
{
|
|
|
|
if (pending->section == SECTION_UNKNOWN)
|
|
|
|
pending->section = desired_section;
|
|
|
|
else if (pending->section != desired_section)
|
|
|
|
fatal("label '%s' can't change sections", pending->name);
|
|
|
|
|
2016-10-10 22:12:11 +00:00
|
|
|
fprintf(outputfile, "\n.sect %s\n", section_to_str(pending->section));
|
2016-10-27 21:17:16 +00:00
|
|
|
fprintf(outputfile, "%s:\n", platform_label(pending->name));
|
2016-09-18 21:24:54 +00:00
|
|
|
pending = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-29 10:55:21 +00:00
|
|
|
static void writehex(arith data, int size)
|
|
|
|
{
|
|
|
|
if (data < 0)
|
|
|
|
fprintf(outputfile, "-0x%0*lx", size*2, -data);
|
|
|
|
else
|
|
|
|
fprintf(outputfile, "0x%0*lx", size*2, data);
|
|
|
|
}
|
|
|
|
|
2016-09-18 21:24:54 +00:00
|
|
|
void data_int(arith data, size_t size, bool is_ro)
|
|
|
|
{
|
|
|
|
emit_header(is_ro ? SECTION_ROM : SECTION_DATA);
|
|
|
|
assert((size == 1) || (size == 2) || (size == 4) || (size == 8));
|
2018-09-19 22:12:03 +00:00
|
|
|
fprintf(outputfile, "\t.data%ld ", size);
|
2016-10-29 10:55:21 +00:00
|
|
|
writehex(data, size);
|
|
|
|
fprintf(outputfile, "\n");
|
2016-09-18 21:24:54 +00:00
|
|
|
}
|
|
|
|
|
2016-10-29 10:55:21 +00:00
|
|
|
|
2016-10-18 20:29:42 +00:00
|
|
|
void data_float(const char* data, size_t size, bool is_ro)
|
|
|
|
{
|
|
|
|
unsigned char buffer[8];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
emit_header(is_ro ? SECTION_ROM : SECTION_DATA);
|
|
|
|
assert((size == 4) || (size == 8));
|
|
|
|
|
2018-09-19 22:12:03 +00:00
|
|
|
fprintf(outputfile, "\t.dataf%ld %s\n", size, data);
|
2016-10-18 20:29:42 +00:00
|
|
|
}
|
|
|
|
|
2022-07-16 23:39:26 +00:00
|
|
|
static bool istext(char c)
|
2016-10-27 19:48:46 +00:00
|
|
|
{
|
|
|
|
return isprint(c) && (c != '"');
|
|
|
|
}
|
|
|
|
|
2016-09-18 21:24:54 +00:00
|
|
|
void data_block(const uint8_t* data, size_t size, bool is_ro)
|
|
|
|
{
|
|
|
|
const uint8_t* start = data;
|
|
|
|
const uint8_t* end = data + size;
|
|
|
|
const uint8_t* p = data;
|
|
|
|
|
|
|
|
emit_header(is_ro ? SECTION_ROM : SECTION_DATA);
|
|
|
|
|
|
|
|
start = p = data;
|
|
|
|
while (p < end)
|
|
|
|
{
|
2016-10-27 19:48:46 +00:00
|
|
|
while ((p < end) && istext(*p))
|
2016-09-18 21:24:54 +00:00
|
|
|
p++;
|
|
|
|
|
|
|
|
if (start < p)
|
|
|
|
{
|
2016-10-10 22:12:11 +00:00
|
|
|
fprintf(outputfile, "\t.ascii \"");
|
2016-09-18 21:24:54 +00:00
|
|
|
while (start < p)
|
|
|
|
{
|
2016-10-10 22:12:11 +00:00
|
|
|
fprintf(outputfile, "%c", *start);
|
2016-09-18 21:24:54 +00:00
|
|
|
start++;
|
|
|
|
}
|
2016-10-10 22:12:11 +00:00
|
|
|
fprintf(outputfile, "\"\n");
|
2016-09-18 21:24:54 +00:00
|
|
|
}
|
|
|
|
|
2016-10-27 19:48:46 +00:00
|
|
|
while ((p < end) && !istext(*p))
|
2016-09-18 21:24:54 +00:00
|
|
|
p++;
|
|
|
|
|
|
|
|
if (start < p)
|
|
|
|
{
|
|
|
|
bool first = true;
|
|
|
|
|
2016-10-10 22:12:11 +00:00
|
|
|
fprintf(outputfile, "\t.data1 ");
|
2016-09-18 21:24:54 +00:00
|
|
|
while (start < p)
|
|
|
|
{
|
|
|
|
if (!first)
|
2016-10-10 22:12:11 +00:00
|
|
|
fprintf(outputfile, ", ");
|
2016-10-29 10:55:21 +00:00
|
|
|
writehex(*start, 1);
|
2016-09-18 21:24:54 +00:00
|
|
|
start++;
|
|
|
|
first = false;
|
|
|
|
}
|
2016-10-10 22:12:11 +00:00
|
|
|
fprintf(outputfile, "\n");
|
2016-09-18 21:24:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void data_offset(const char* label, arith offset, bool is_ro)
|
|
|
|
{
|
|
|
|
emit_header(is_ro ? SECTION_ROM : SECTION_DATA);
|
2018-09-19 22:12:03 +00:00
|
|
|
fprintf(outputfile, "\t.data%d %s+%ld\n",
|
2016-10-31 22:21:33 +00:00
|
|
|
EM_pointersize, platform_label(label), offset);
|
2016-09-18 21:24:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void data_bss(arith size, int init)
|
|
|
|
{
|
|
|
|
if (init != 0)
|
|
|
|
fatal("non-zero-initialised bss not supported");
|
|
|
|
|
|
|
|
emit_header(SECTION_BSS);
|
2018-09-19 22:12:03 +00:00
|
|
|
fprintf(outputfile, "\t.space %ld\n", size);
|
2016-09-18 21:24:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* vim: set sw=4 ts=4 expandtab : */
|