76 lines
1.1 KiB
C
76 lines
1.1 KiB
C
#include <stdio.h>
|
|
#include "rifle/mu.h"
|
|
|
|
#define retreg(msize) \
|
|
(msize >= MSIZE_MAX) \
|
|
? "bad" \
|
|
: rettab[(msize)]
|
|
|
|
/* Size to return register table */
|
|
static const char *rettab[] = {
|
|
[MSIZE_BYTE] = "al",
|
|
[MSIZE_WORD] = "ax",
|
|
[MSIZE_DWORD] = "eax",
|
|
[MSIZE_QWORD] = "rax"
|
|
};
|
|
|
|
int
|
|
mu_gen_label(struct rifle_state *state, const char *name, bool global)
|
|
{
|
|
if (state == NULL || name == NULL) {
|
|
return -1;
|
|
}
|
|
|
|
if (global) {
|
|
fprintf(
|
|
state->out_fp,
|
|
"[global %s]\n",
|
|
name
|
|
);
|
|
}
|
|
|
|
fprintf(
|
|
state->out_fp,
|
|
"%s:\n",
|
|
name
|
|
);
|
|
|
|
return 0;
|
|
}
|
|
|
|
int
|
|
mu_gen_retimm(struct rifle_state *state, ssize_t v, msize_t size)
|
|
{
|
|
if (state == NULL) {
|
|
return -1;
|
|
}
|
|
|
|
if (size >= MSIZE_MAX) {
|
|
return -1;
|
|
}
|
|
|
|
fprintf(
|
|
state->out_fp,
|
|
INST("mov %s, %zd"),
|
|
retreg(size),
|
|
v
|
|
);
|
|
|
|
return 0;
|
|
}
|
|
|
|
int
|
|
mu_gen_ret(struct rifle_state *state)
|
|
{
|
|
if (state == NULL) {
|
|
return -1;
|
|
}
|
|
|
|
fprintf(
|
|
state->out_fp,
|
|
INST("ret")
|
|
);
|
|
|
|
return 0;
|
|
}
|