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

(1) プリプロセッサは善か悪か?(この部分はa4_p0007と同じ)


(2) プログラム p0008a.c について #0 (プリプロセッサ)(a4_p0007と同じ)

(3) プログラム p0008a.c について #1 (コマンドライン計算機)(a4_p0007と同じ)

(4) プログラム p0008a.c について #2 (簡易Cコンパイラ)

(5) プログラム p0008a.c について #3 (C言語JITコンパイラ)

(6) プログラム p0008a.c について #4 (C言語インタプリタ)(a4_p0007と同じ)

(7) 主張のまとめ

8.0KB(参考用) printf("hello, world\n"); のみのプログラム
23.5KBMode=0: ppのみ
28.0KBMode=1: pp+cc
30.0KBMode=2: pp+cc+cjit
35.5KBMode=3: pp+cc+cjit+clang
37.5KBMode=4: pp+cc+cjit+clang+calc+graphic(=全部)

(8) ダウンロード

(9) p0008a.c のソースコード

#define a_Version   1
#include <acl4v1.c>
#include <windows.h>
#include "a4_t0002.c"
#include "a4_t0003.c"

#define Mode	4
// 0:pp-only, 1:pp+cc, 2:pp+cc+cjit, 3:all-graphic 4:all

#if (Mode >= 4)
    a_Win *win;
    void openWin(intptr_t x, intptr_t y) { win = a_malloc(_a_  sizeof (a_Win)); a_Win_ini(_a_  win, (int) x, (int) y, "graph", 0x000000); }
    void setPix(intptr_t x, intptr_t y, intptr_t c) { win->buf[x + y * win->xsz] = (uint32_t) c; }
    void flushWin() { a_Win_flushAll0(win); }
    void waitInf() { if (win != NULL) { flushWin(); } for (;;) Sleep(1000); }
#endif

int main(int argc, const char **argv)
{
    const char *src = getArg(argc, argv, "src:", "");
    int dbg = strtol(getArg(argc, argv, "dbg:", "0"), NULL, 0);
    VecChr vc0[1], vc1[1]; VecChr_iniArg(_a_ vc0, src, 2); VecChr_ini(_a_ vc1);
    #if (Mode >= 2)
        static a_AsmDbConstTable tbl[5] = {
            { "printf",   0, 0, (intptr_t) printf   },
            #if (Mode >= 4)
                { "openWin",  0, 0, (intptr_t) openWin  },
                { "setPix",   0, 0, (intptr_t) setPix   },
                { "flushWin", 0, 0, (intptr_t) flushWin },
                { "waitInf",  0, 0, (intptr_t) waitInf  },
            #endif
        };
        intptr_t binSz = 64 * 1024; char *bin = a_mallocRWX(binSz); BufFree bf[1]; BufFree_ini(_a_ bf);
    #endif

    if (strtol(getArg(argc, argv, "pp:", "0"), NULL, 0) != 0) {
        int mod = strtol(getArg(argc, argv, "mod:", "1"), NULL, 0);
        a_convVc_pp(vc0, vc1, 1 + 2 + mod * 16, 1, "%.*s", stdout, src);
    }

    #if (Mode >= 1)
        if (strtol(getArg(argc, argv, "cc:", "0"), NULL, 0) != 0) {
            a_convVc_pp(vc0, vc1, 1 + 2 + 2 * 16, dbg, "[preprocessed]\n%.*s\n", stdout, src);
            a_convVc_clang_clout(vc1, vc0, 1); if (dbg != 0) printf("[clout]\n%s\n", vc0->p);
            VecChr_N(vc1) = 0; VecChr_printf(vc1, "Ent_III(256,256,0);\n%sLev_III(256,256,0);\n", vc0->p);
            a_convVc_pp(vc1, vc0, 1 + 2 * 16, dbg, "[asm_a4vm]\n%.*s\n",     stdout, "conv_clout_a4vm");
            a_convVc_pp(vc0, vc1, 1 + 2 * 16, dbg, "[asm_x86-32-0]\n%.*s\n", stdout, "conv_a4vm_asmX86Bit32Typ0");
            a_convVc_pp(vc1, vc0, 1 + 2 * 16, 1,   "%.*s\n",                 stdout, "conv_asmX86Bit32Typ0_Typ1");
        }
    #endif

    #if (Mode >= 2)
        if (strtol(getArg(argc, argv, "cjit:", "0"), NULL, 0) != 0) {
            a_convVc_pp(vc0, vc1, 1 + 2 + 2 * 16, dbg, "[preprocessed]\n%.*s\n", stdout, src);
            a_convVc_clang_clout(vc1, vc0, 1); if (dbg != 0) printf("[clout]\n%s\n", vc0->p);
            VecChr_N(vc1) = 0; VecChr_printf(vc1, "Ent_III(256,256,0);\n%sLev_III(256,256,0);\n", vc0->p);
            a_convVc_pp(vc1, vc0, 1 + 2 * 16, dbg, "[asm_a4vm]\n%.*s\n",     stdout, "conv_clout_a4vm");
            a_convVc_pp(vc0, vc1, 1 + 2 * 16, dbg, "[asm_x86-32-0]\n%.*s\n", stdout, "conv_a4vm_asmX86Bit32Typ0");
            a_convVc_pp(vc1, vc0, 1 + 2 * 16, dbg, "[asm_db]\n%.*s\n",       stdout, "conv_asmX86Bit32Typ0_asmDb");
            intptr_t bn = a_conv_asmDb_bin(vc0->p, VecChr_N(vc0), bin, binSz, tbl, sizeof tbl / sizeof (a_AsmDbConstTable), bf);
            if (bn > 0) {
                if (dbg != 0) { printf("[binary]\n"); hexDump(bin, bn, stdout); }
                void (*pf)(); pf = (void (*)()) bin; pf();
            }
        }
    #endif

    #if (Mode >= 3)
        if (strtol(getArg(argc, argv, "clang:", "0"), NULL, 0) != 0) {
            a_convVc_pp(vc0, vc1, 1 + 2 + 2 * 16, dbg, "[preprocessed]\n%.*s\n", stdout, src);
            a_convVc_clang_clout(vc1, vc0, 1); if (dbg != 0) printf("[clout]\n%s\n", vc0->p);
            VecChr_N(vc1) = 0; VecChr_printf(vc1, "Ent_III(256,256,0);\n%sLev_III(256,256,0);\n", vc0->p);
            a_convVc_pp(vc1, vc0, 1 + 2 * 16, dbg, "[asm_a4vm]\n%.*s\n", stdout, "conv_clout_a4vm");
            a_convVc_pp(vc0, vc1, 1 + 2 * 16, dbg, "[asm_db]\n%.*s\n",   stdout, "conv_a4vm_asmDb");
            intptr_t bn = a_conv_asmDb_bin(vc1->p, a_VecChr_N(vc1), bin, binSz / sizeof(intptr_t), tbl, sizeof tbl / sizeof (a_AsmDbConstTable), bf);
            intptr_t a4arg[10]; a4arg[0] = 0; A4vm_exec0((intptr_t *) bin, bn, 10, a4arg);
        }
    #endif

    #if (Mode >= 4)
        if (strtol(getArg(argc, argv, "calc:", "0"), NULL, 0) != 0) {
            a_convVc_pp(vc0, vc1, 1 + 2 + 2 * 16, dbg, "[preprocessed]\n%.*s\n", stdout, src);
            Preprocessor_Eval ev[1]; Preprocessor_Eval_ini(_a_ ev); ev->err = 0;
            Token0 t0[1]; Token0_ini1(t0); t0->s = vc1->p; t0->s1 = vc1->p + VecChr_N(vc1);
            for (;;) {
                const char *t = Token1_get(t0);
                if (t0->c == ',' || t0->c == ';') { printf("%c ", t0->c); continue; }
                if (t0->c == 0) break;
                t0->s = t; intptr_t i = Preprocessor_eval(ev, t0, 0x7fff);
                if (ev->err != 0) break;
                printf("%d", (int) i);
            }
            printf("\n"); Preprocessor_Eval_din(_a_ ev);
        }
    #endif

    #if (Mode >= 2)
        BufFree_flush(_a_ bf); BufFree_din(_a_ bf);
    #endif
    VecChr_din4(_a_ vc0, vc1, 0, 0);
    a_malloc_debugList(_a); a_DbgObjInfTbl_debugList(_a);
    return 0;
}

(99) 更新履歴


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