blob: 66adf6857c707fc181eadb8add296b63cd4b0ad7 [file] [log] [blame]
Colin Cross7bb052a2015-02-03 12:59:37 -08001// Copyright 2012 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5#include "a.h"
6
7/*
8 * Helpers for building cmd/gc.
9 */
10
11// gcopnames creates opnames.h from go.h.
12// It finds the OXXX enum, pulls out all the constants
13// from OXXX to OEND, and writes a table mapping
14// op to string.
15void
16gcopnames(char *dir, char *file)
17{
18 char *p, *q;
19 int i, j, end;
20 Buf in, b, out;
21 Vec lines, fields;
22
23 binit(&in);
24 binit(&b);
25 binit(&out);
26 vinit(&lines);
27 vinit(&fields);
28
29 bwritestr(&out, bprintf(&b, "// auto generated by go tool dist\n"));
30 bwritestr(&out, bprintf(&b, "static char *opnames[] = {\n"));
31
32 readfile(&in, bprintf(&b, "%s/go.h", dir));
33 splitlines(&lines, bstr(&in));
34 i = 0;
35 while(i<lines.len && !contains(lines.p[i], "OXXX"))
36 i++;
37 end = 0;
38 for(; i<lines.len && !end; i++) {
39 p = xstrstr(lines.p[i], "//");
40 if(p != nil)
41 *p = '\0';
42 end = contains(lines.p[i], "OEND");
43 splitfields(&fields, lines.p[i]);
44 for(j=0; j<fields.len; j++) {
45 q = fields.p[j];
46 if(*q == 'O')
47 q++;
48 p = q+xstrlen(q)-1;
49 if(*p == ',')
50 *p = '\0';
51 bwritestr(&out, bprintf(&b, " [O%s] = \"%s\",\n", q, q));
52 }
53 }
54
55 bwritestr(&out, bprintf(&b, "};\n"));
56
57 writefile(&out, file, 0);
58
59 bfree(&in);
60 bfree(&b);
61 bfree(&out);
62 vfree(&lines);
63 vfree(&fields);
64}
65
66// mkanames reads [568].out.h and writes anames[568].c
67// The format is much the same as the Go opcodes above.
68// it also writes out cnames array for C_* constants.
69void
70mkanames(char *dir, char *file)
71{
72 int i, j, ch;
73 Buf in, b, out, out2;
74 Vec lines;
75 char *p;
76
77 binit(&b);
78 binit(&in);
79 binit(&out);
80 binit(&out2);
81 vinit(&lines);
82
83 ch = file[xstrlen(file)-3];
84 bprintf(&b, "%s/../cmd/%cl/%c.out.h", dir, ch, ch);
85 readfile(&in, bstr(&b));
86 splitlines(&lines, bstr(&in));
87
88 // Include link.h so that the extern declaration there is
89 // checked against the non-extern declaration we are generating.
90 bwritestr(&out, bprintf(&b, "#include <u.h>\n"));
91 bwritestr(&out, bprintf(&b, "#include <libc.h>\n"));
92 bwritestr(&out, bprintf(&b, "#include <bio.h>\n"));
93 bwritestr(&out, bprintf(&b, "#include <link.h>\n"));
94 bwritestr(&out, bprintf(&b, "\n"));
95
96 bwritestr(&out, bprintf(&b, "char* anames%c[] = {\n", ch));
97 for(i=0; i<lines.len; i++) {
98 if(hasprefix(lines.p[i], "\tA")) {
99 p = xstrstr(lines.p[i], ",");
100 if(p)
101 *p = '\0';
102 p = xstrstr(lines.p[i], "\n");
103 if(p)
104 *p = '\0';
105 p = lines.p[i] + 2;
106 bwritestr(&out, bprintf(&b, "\t\"%s\",\n", p));
107 }
108 }
109 bwritestr(&out, "};\n");
110
111 j=0;
112 bprintf(&out2, "char* cnames%c[] = {\n", ch);
113 for(i=0; i<lines.len; i++) {
114 if(hasprefix(lines.p[i], "\tC_")) {
115 p = xstrstr(lines.p[i], ",");
116 if(p)
117 *p = '\0';
118 p = xstrstr(lines.p[i], "\n");
119 if(p)
120 *p = '\0';
121 p = lines.p[i] + 3;
122 bwritestr(&out2, bprintf(&b, "\t\"%s\",\n", p));
123 j++;
124 }
125 }
126 bwritestr(&out2, "};\n");
127 if(j>0)
128 bwriteb(&out, &out2);
129
130 writefile(&out, file, 0);
131
132 bfree(&b);
133 bfree(&in);
134 bfree(&out);
135 bfree(&out2);
136 vfree(&lines);
137}