Skip to content

Instantly share code, notes, and snippets.

@keiichiw
Created December 3, 2016 09:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keiichiw/4c222b962194e257efa1e47b92b5adba to your computer and use it in GitHub Desktop.
Save keiichiw/4c222b962194e257efa1e47b92b5adba to your computer and use it in GitHub Desktop.
ELVMのCバックエンド・constexprバックエンドの出力例
# 出典: https://github.com/shinh/elvm/blob/master/test/05regjmp.eir
mov A, l
jmp A
putc 78
exit
l:
putc 89
exit
// ELVM Cバックエンドの出力
#include <stdio.h>
#include <stdlib.h>
// レジスタとメモリをグローバル変数として用意
unsigned int a;
unsigned int b;
unsigned int c;
unsigned int d;
unsigned int bp;
unsigned int sp;
unsigned int pc;
unsigned int mem[1<<24];
void func0() {
while (0 <= pc && pc < 512) {
switch (pc) { //プログラムカウンタの値によって、switch
case -1: /* dummy */
break;
case 0:
if (1) pc = 1 - 1;
break;
case 1:
a = 3;
if (1) pc = a - 1;
break;
case 2:
putchar(78);
exit(0);
break;
case 3:
putchar(89);
exit(0);
}
pc++;
}
}
int main() {
mem[0] = 1;
while (1) {
switch (pc / 512 | 0) {
case 0:
func0();
break;
}
}
return 1;
}
// ELVM C++ constexprバックエンドの出力
#include <cstdio>
#include <utility>
const size_t BUF_SIZE = 10000;
constexpr char input[] =
#include "input.txt"
;
struct buffer {
unsigned int size;
unsigned int b[BUF_SIZE];
};
template <size_t... I>
constexpr buffer make_buf_impl(unsigned int size, unsigned int* buf, std::index_sequence<I...>) {
return buffer{size, {buf[I]...}};
}
constexpr buffer make_buf(unsigned int size, unsigned int* buf) {
return make_buf_impl(size, buf, std::make_index_sequence<BUF_SIZE>{});
}
constexpr buffer constexpr_main() {
unsigned int a = 0;
unsigned int b = 0;
unsigned int c = 0;
unsigned int d = 0;
unsigned int bp = 0;
unsigned int sp = 0;
unsigned int pc = 0;
unsigned int i_cur = 0;
unsigned int o_cur = 0;
unsigned int mem[1<<24] = {0};
unsigned int buf[BUF_SIZE] = {0};
mem[0] = 1;
while (1) {
switch (pc) {
case 0:
if (1) pc = 1 - 1;
break;
case 1:
a = 3;
if (1) pc = a - 1;
break;
case 2:
buf[o_cur++] = 78;
return make_buf(o_cur, buf);
break;
case 3:
buf[o_cur++] = 89;
return make_buf(o_cur, buf);
}
pc++;
}
return make_buf(o_cur, buf);
}
int main() {
constexpr buffer buf = constexpr_main();
constexpr unsigned int output_size = buf.size;
for(int i = 0; i < output_size; ++i) {
putchar(buf.b[i]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment