aboutsummaryrefslogtreecommitdiff
path: root/src/prc1/cgen/qbe/print.c
blob: 2890eb8317e0f5b39596c56a78a693863aeccda5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "cgen.h"
#include "cgen/prv.h"
#include "print.h"
#include "qbe.h"
#include <stdio.h>

static int cg_str(const struct prisym *s, struct cgen *c, const char *f)
{
    fprintf(stderr, "%s: TODO\n", __func__);
    return -1;
}

static int cg_ptr(const struct prisym *s, struct cgen *c, const char *f)
{
    const char *tmpf = cgen_tmp2(c), *tmpv = cgen_tmp2(c),
        *name = s->s->tk->s;

    if (!tmpf || !tmpv)
        return -1;

    printf("%%%s =l loadl ", tmpv);

    if (cgen_global(c->fn, s->u.e))
        printf("$%s", name);
    else
        printf("%%%s_", name);

    fputc('\n', stdout);
    printf("%%%s =l loadl $%s\n", tmpf, f);
    printf("call $fprintf(w %%%s, l $____fmtlx, ..., l %%%s)\n", tmpf, tmpv);
    return 0;
}

static int cg_num(const struct prisym *s, struct cgen *c, const char *f)
{
    fprintf(stderr, "%s: TODO\n", __func__);
    return -1;
}

static int cg_nam(const struct prisym *ps, struct cgen *c, const char *f)
{
    const char *tmpf = cgen_tmp2(c), *name = ps->u.name;

    if (!tmpf)
        return -1;

    printf("%%%s =l loadl $%s\n", tmpf, f);
    printf("call $fprintf(w %%%s, l $____fmtlx, ..., l $%s)\n", tmpf, name);
    return 0;
}

static int cg_lit(const struct prisym *s, struct cgen *c, const char *f)
{
    const struct lit *l = s->u.lit;
    const char *tmp;

    if (!l)
    {
        fprintf(stderr, "%s: unexpected null literal\n", __func__);
        return -1;
    }

    /* TODO: ask target for sizeof (void *) */
    tmp = cgen_tmp(sizeof (void *));
    printf("%%%s =l loadl $%s\n", tmp, f);
    printf("call $fputs(l $%s, w %%%s)\n", l->name, tmp);
    return 0;
}

int qbe_print(const struct print *pr, struct cgen *c, const char *f)
{
    for (size_t i = 0; i < pr->nsyms; i++)
    {
        const struct prisym *ps = &pr->syms[i];
        static int (*fn[])(const struct prisym *, struct cgen *, const char *) =
        {
            [PRS_LIT] = cg_lit,
            [PRS_STR] = cg_str,
            [PRS_NAM] = cg_nam,
            [PRS_PTR] = cg_ptr,
            [PRS_NUM] = cg_num
        };

        if (fn[ps->type](ps, c, f))
            return -1;
    }

    if (!pr->skipln)
    {
        printf("%%v =l loadl $%s\n", f);
        puts("call $fputc(l 10 , w %v)");
    }

    return 0;
}