acl4のプログラムのページ0005

(1) 実行したい

(2) p0005a.c [59行] : 即興で作った仮想マシン

#define a_Version 1
#include <acl4.c>

void myPut(Preprocessor_Put0 *w, VecChr *lin) // t0013b.cで作ったやつと同じ.
{
    if (Preprocessor_isDirective(lin->p, lin->p + lin->n) != 0)
        fprintf(stderr, "[err] %.*s", (int) lin->n, lin->p);
    else
        VecChr_puts(w->dst, lin->p, lin->p + lin->n);
}

int main(int argc, const char **argv)
{
    if (argc < 2) return 1;
    Preprocessor pp[1]; Preprocessor_ini(pp);                            // プリプロセッサの初期化.
    pp->put = (void *) myPut;                                            // 出力関数の変更.
    Preprocessor_SourceFiles_addFile(pp->sfs, argv[1], strlen(argv[1])); // 入力ファイルを指定.
    Preprocessor_SourceFiles_addFile(pp->sfs, "a4vm-asm-p0005a.h", 17);  // 最初にこれをインクロードさせる.
    VecChr vc[1]; VecChr_ini(vc); pp->put0->dst = vc;                    // 出力先のオブジェクトを指定.
    Preprocessor_main(pp); Preprocessor_din(pp);                         // プリプロセッサ処理. およびメモリ開放.

    Token0 t0[1]; Token0_ini1(t0);
    t0->s = vc->p; t0->s1 = vc->p + vc->n;
    Preprocessor_Eval ev[1]; ev->err = 0;
    VecChr bin[1]; VecChr_ini(bin);
    for (;;) {
        intptr_t i = Preprocessor_eval(ev, t0, 0x7fff);
        if (ev->err != 0) break;
        VecChr_resizeDiff(_arg_  bin, 1); bin->p[bin->n - 1] = (char) i;
        Token1_get(t0);
        if (t0->c != ',') break;
    }

    int32_t *bp = (int32_t *) bin->p, bn = bin->n / sizeof (int32_t), reg[256], lab[256], pc;
    for (pc = 0; pc < bn; pc += 4) {
        if (bp[pc] == 0x02)
            lab[bp[pc + 1]] = pc + 4;  // Lbl_T(t)命令の次の命令を指す.
	 }
	 for (pc = 0;;) {
            int32_t op = bp[pc], a = bp[pc + 1], b = bp[pc + 2], c = bp[pc + 3]; pc += 4;
            switch (op) {
            case 0x01: reg[a] = b;                       continue; // Lod_RI(r,i)
            case 0x02:                                   continue; // Lbl_T(t)
            case 0x03: reg[a] += reg[b];                 continue; // Add_RR(r,s)
            case 0x04: reg[a] += b;                      continue; // Add_RI(r,i)
            case 0x05: if (reg[a] <= b) { pc = lab[c]; } continue; // Jle_RIT(r,i,t)
            case 0x06: if (reg[a] <  b) { pc = lab[c]; } continue; // Jlt_RIT(r,i,t)
            case 0x10: goto fin;
            case 0x11: goto fin;
            case 0x12: putchar(reg[a]);                  continue; // Dbg_Putc_R(r)
            case 0x13: putchar(a);                       continue; // Dbg_Putc_I(i)
            case 0x16: printf("%d", reg[a]);             continue; // Dbg_PutDec_R(r)
        }
    }
fin:
    VecChr_din4(vc, bin, 0, 0);
    a_malloc_debugList(_arg);
    return 0;
}

(3) 実行結果

>p0005a p0004a0.txt
 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~

(99) 更新履歴


トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS