8 static int find_protostr(char *dst, size_t dlen, FILE *fhdr,
9 const char *sym, int *pline)
14 while (fgets(dst, dlen, fhdr))
17 if (strstr(dst, sym) != NULL)
25 p = dst + strlen(dst);
26 for (p--; p > dst && my_isblank(*p); --p)
32 static int get_regparm(char *dst, size_t dlen, char *p)
39 for (o = 0, i = 1; o < dlen; i++) {
50 static const char *known_types[] = {
61 static int check_type(const char *name)
65 for (i = 0; i < sizeof(known_types) / sizeof(known_types[0]); i++) {
66 l = strlen(known_types[i]);
67 if (strncmp(known_types[i], name, l) == 0)
74 int main(int argc, char *argv[])
76 FILE *fout, *fsyms, *fhdr;
84 int first_regparm = 0;
91 // -c - patch callsites
92 printf("usage:\n%s <bridge.s> <symf> <hdrf>\n",
98 fhdr = fopen(hdrfn, "r");
99 my_assert_not(fhdr, NULL);
101 fsyms = fopen(argv[2], "r");
102 my_assert_not(fsyms, NULL);
104 fout = fopen(argv[1], "w");
105 my_assert_not(fout, NULL);
107 fprintf(fout, ".text\n\n");
109 while (fgets(line, sizeof(line), fsyms))
111 next_word(sym, sizeof(sym), line);
112 if (sym[0] == 0 || sym[0] == ';' || sym[0] == '#')
115 ret = find_protostr(protostr, sizeof(protostr), fhdr,
118 printf("%s: sym '%s' is missing\n",
124 if (p[0] == '/' && p[1] == '/') {
125 printf("warning: decl for sym '%s' is commented out\n", sym);
131 printf("%s:%d:%ld: unhandled return in '%s'\n",
132 hdrfn, pline, (p - protostr) + 1, protostr);
138 // ignore calling convention specifier, for now
139 p = next_word(buf, sizeof(buf), p);
142 printf("%s:%d:%ld: cconv missing\n",
143 hdrfn, pline, (p - protostr) + 1);
147 p = next_idt(buf, sizeof(buf), p);
150 printf("%s:%d:%ld: func name missing\n",
151 hdrfn, pline, (p - protostr) + 1);
155 ret = get_regparm(regparm, sizeof(regparm), p);
157 if (strcmp(regparm, "eax") && strcmp(regparm, "ax")) {
158 printf("%s:%d:%ld: bad regparm: %s\n",
159 hdrfn, pline, (p - protostr) + 1, regparm);
167 printf("%s:%d:%ld: '(' expected, got '%c'\n",
168 hdrfn, pline, (p - protostr) + 1, *p);
173 fprintf(fout, ".global _asm_%s\n", sym);
174 fprintf(fout, "_asm_%s:\n", sym);
186 printf("%s:%d:%ld: unhandled type for arg%d\n",
187 hdrfn, pline, (p - protostr) + 1, xarg);
193 p = next_idt(buf, sizeof(buf), p);
196 printf("%s:%d:%ld: idt missing for arg%d\n",
197 hdrfn, pline, (p - protostr) + 1, xarg);
202 ret = get_regparm(regparm, sizeof(regparm), p);
208 fprintf(fout, "\t movl %d(%%esp), %%%s\n",
212 first_regparm = have_regparm;
213 else if (have_regparm != first_regparm) {
214 printf("%s:%d:%ld: mixed regparm is unhandled\n",
215 hdrfn, pline, (p - protostr) + 1);
220 fprintf(fout, "\t jmp %s\n\n", sym);