backend+mu: Add helper to return immediate values
Signed-off-by: Ian Moffett <ian@mirocom.org>
This commit is contained in:
@@ -1,6 +1,19 @@
|
||||
#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)
|
||||
{
|
||||
@@ -25,6 +38,27 @@ mu_gen_label(struct rifle_state *state, const char *name, bool global)
|
||||
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)
|
||||
{
|
||||
|
||||
@@ -20,6 +20,18 @@
|
||||
*/
|
||||
int mu_gen_label(struct rifle_state *state, const char *name, bool global);
|
||||
|
||||
/*
|
||||
* Generate a register fill corresponding to the platform return
|
||||
* register along side a return instruction.
|
||||
*
|
||||
* @state: Compiler state
|
||||
* @v: Value to return
|
||||
* @size: Return size
|
||||
*
|
||||
* Returns zero on success
|
||||
*/
|
||||
int mu_gen_retimm(struct rifle_state *state, ssize_t v, msize_t size);
|
||||
|
||||
/*
|
||||
* Generate a return instruction
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user