summaryrefslogtreecommitdiff
path: root/6/gen.c
blob: 76d15f5777261c64778c38d47e656970a815339f (plain)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <inttypes.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#include "parse.h"
#include "mi.h"
#include "asm.h"
#include "../config.h"

static int islocal(Node *dcl)
{
    if (dcl->decl.vis != Visintern)
        return 0;
    if (dcl->decl.isimport || dcl->decl.isextern)
        return 0;
    return 1;
}

static int nextlbl;
char *genlocallblstr(char *buf, size_t sz)
{
    if (asmsyntax == Plan9)
        snprintf(buf, 128, ".L%d<>", nextlbl++);
    else
        snprintf(buf, 128, ".L%d", nextlbl++);
    return buf;
}

char *genlblstr(char *buf, size_t sz)
{
    snprintf(buf, 128, ".L%d", nextlbl++);
    return buf;
}

/* 
 * For x86, the assembly names are generated as follows:
 *      local symbols: .name
 *      un-namespaced symbols: <symprefix>name
 *      namespaced symbols: <symprefix>namespace$name
 *      local symbols on plan9 have the file-unique suffix '<>' appended
 */
char *asmname(Node *dcl)
{
    char buf[1024];
    char *vis, *pf, *ns, *name, *sep;
    Node *n;

    n = dcl->decl.name;
    pf = Symprefix;
    ns = n->name.ns;
    name = n->name.name;
    vis = "";
    sep = "";
    if (asmsyntax == Plan9)
        if (islocal(dcl))
            vis = "<>";
    if (!ns || !ns[0])
        ns = "";
    else
        sep = "$";
    if (name[0] == '.')
        pf = "";

    snprintf(buf, sizeof buf, "%s%s%s%s%s", pf, ns, sep, name, vis);
    return strdup(buf);
}

char *tydescid(char *buf, size_t bufsz, Type *ty)
{
    char *sep, *ns;

    sep = "";
    ns = "";
    if (ty->type == Tyname) {
        if (ty->name->name.ns) {
            ns = ty->name->name.ns;
            sep = "$";
        }
        if (ty->vis == Visexport || ty->isimport)
            snprintf(buf, bufsz, "_tydesc$%s%s%s", ns, sep, ty->name->name.name);
        else
            snprintf(buf, bufsz, "_tydesc$%s%s%s$%d", ns, sep, ty->name->name.name, ty->tid);
    } else {
        if (file->file.globls->name) {
            ns = file->file.globls->name;
            sep = "$";
        }
        snprintf(buf, bufsz, "_tydesc%s%s$%d",sep, ns, ty->tid);
    }
    return buf;
}

void gen(Node *file, char *out)
{
    switch (asmsyntax) {
        case Plan9:     genp9(file, out);       break;
        case Gnugas:    gengas(file, out);      break;
        default:        die("unknown target");  break;
    }
}