as.c (7072B)
1 #include "../config.h" 2 3 #include <assert.h> 4 #include <stdio.h> 5 #include <stdlib.h> /* qsort */ 6 #include <string.h> 7 #include <unistd.h> /* isatty */ 8 9 #include "arch.h" 10 #include "format.h" 11 #include "ir_asm.h" 12 #include "parse_asm.h" 13 #include "table.h" 14 #include "tcutil.h" 15 16 #define PROG_START (0x100) 17 #define START_ADDRESS (0x01000000 + PROG_START) 18 #define LOAD_ADDRESS START_ADDRESS 19 20 static int 21 read_line_comment_skip(FILE *fp, struct ParseInfo *info) 22 { 23 int block_comment = 0; 24 int wait_line_end = 1; 25 const char *p = info->p; 26 for (;;) { 27 if (p == NULL) { 28 ++info->lineno; 29 char *rawline = NULL; 30 size_t capa = 0; 31 ssize_t len = getline_chomp(&rawline, &capa, fp); 32 if (len == -1) { /* EOF */ 33 info->rawline = info->p = NULL; 34 if (block_comment) 35 parse_error(info, "block comment not closed"); 36 return 0; 37 } 38 info->rawline = p = rawline; 39 wait_line_end = 0; 40 } 41 42 if (!block_comment) { 43 const char *q = block_comment_start(p); 44 if (q != NULL) { 45 block_comment = 1; 46 p = q + 2; 47 } 48 } 49 if (block_comment) { 50 p = block_comment_end(p); 51 if (p != NULL) 52 block_comment = 0; 53 wait_line_end = 1; 54 continue; /* continue block comment */ 55 } 56 57 p = skip_whitespaces(p); 58 if (*p == '/' && p[1] == '/') { /* struct line comment */ 59 p = NULL; 60 continue; 61 } 62 63 if (wait_line_end) { 64 if (*p != '\0') 65 parse_error(info, "line end expected"); 66 p = NULL; 67 continue; 68 } 69 70 break; 71 } 72 73 /* struct line */ 74 info->p = p; 75 return 1; 76 } 77 78 static void 79 parse_file(FILE *fp, struct ParseInfo *info) 80 { 81 info->lineno = 0; 82 info->rawline = info->p = NULL; 83 info->prefetched = NULL; 84 set_current_section(info, kSecText, kSegText, SF_EXECUTABLE); 85 86 for (;;) { 87 if (!read_line_comment_skip(fp, info)) 88 break; 89 90 struct SectionInfo *section = info->current_section; 91 struct Vector *irs = section->irs; 92 struct Line line; 93 if (!parse_line(&line, info)) 94 continue; 95 96 if (line.label != NULL) { 97 vec_push(irs, new_ir_label(line.label)); 98 99 if (!add_label_table(info->label_table, line.label, section, 1, 0)) 100 ++info->error_count; 101 } 102 103 if (line.dir != NODIRECTIVE) 104 continue; 105 106 if (line.inst != NULL) { 107 struct Code code; 108 arch.assemble_inst(line.inst, info, &code); 109 if (code.len > 0) 110 vec_push(irs, new_ir_code(&code)); 111 } 112 } 113 } 114 115 static void 116 drop_all(FILE *fp) 117 { 118 for (;;) { 119 char buf[4096]; 120 size_t size = fread(buf, 1, sizeof(buf), fp); 121 if (size < sizeof(buf)) 122 break; 123 } 124 } 125 126 static struct LabelInfo * 127 make_label_referred(struct Table *label_table, const struct Name *label, int und) 128 { 129 struct LabelInfo *label_info = table_get(label_table, label); 130 if (label_info == NULL) { 131 if (!und) 132 return NULL; 133 label_info = add_label_table(label_table, label, NULL, 0, 1); 134 } 135 label_info->flag |= LF_REFERRED; 136 return label_info; 137 } 138 139 static void 140 fix_section_size(struct Vector *sections, uintptr_t start_address) 141 { 142 uintptr_t addr = start_address; 143 for (int i = 0; i < sections->len; ++i) { 144 struct SectionInfo *section = sections->data[i]; 145 if ((section->flag & SF_BSS ? section->bss_size : section->ds->len) <= 0) 146 continue; 147 section->start_address = addr = ALIGN(addr, section->align); 148 addr += (section->flag & SF_BSS) ? section->bss_size : section->ds->len; 149 } 150 } 151 152 static inline int 153 section_key(const struct SectionInfo *p) 154 { 155 int flag = p->flag; 156 /* .text(0), .rodata(1), .data(2), .bss(6) */ 157 return (flag & (SF_BSS | SF_WRITABLE | SF_EXECUTABLE)) ^ SF_EXECUTABLE; 158 } 159 160 static int 161 cmp_section(const void *pa, const void *pb) 162 { 163 const struct SectionInfo *sa = *(const struct SectionInfo **)pa; 164 int ka = section_key(sa); 165 const struct SectionInfo *sb = *(const struct SectionInfo **)pb; 166 int kb = section_key(sb); 167 int d = ka - kb; 168 if (d != 0) 169 return d; 170 return pa < pb ? -1 : 1; 171 } 172 173 static struct Vector * 174 sort_sections(struct Table *section_infos) 175 { 176 struct Vector *sections = new_vector(); 177 const struct Name *name; 178 struct SectionInfo *section; 179 for (int it = 0; (it = table_iterate(section_infos, it, &name, (void **)§ion)) != -1;) 180 vec_push(sections, section); 181 qsort(sections->data, sections->len, sizeof(void *), cmp_section); 182 return sections; 183 } 184 185 static void 186 usage(FILE *fp) 187 { 188 fprintf( 189 fp, 190 "Usage: as [options] file...\n" 191 "struct Options:\n" 192 " -o <filename> Set output filename (Default: " 193 "Standard output)\n" 194 ); 195 } 196 197 int 198 main(int argc, char *argv[]) 199 { 200 enum { 201 OPT_HELP = 128, 202 OPT_VERSION, 203 }; 204 static const struct option options[] = { 205 {"o", required_argument, 'o'}, /* specify output filename */ 206 {"-help", no_argument, OPT_HELP}, 207 {"v", no_argument, OPT_VERSION}, 208 {"-version", no_argument, OPT_VERSION}, 209 {NULL, 0, 0}, 210 }; 211 212 const char *ofn = NULL; 213 int opt; 214 while ((opt = optparse(argc, argv, options)) != -1) { 215 switch (opt) { 216 default: 217 assert(0); 218 break; 219 case OPT_HELP: 220 usage(stdout); 221 exit(0); 222 case OPT_VERSION: 223 show_version("as", TC_TARGET_ARCH); 224 return 0; 225 case 'o': 226 ofn = optarg; 227 break; 228 case '?': 229 fprintf(stderr, "Warning: unknown option: %s\n", argv[optind - 1]); 230 break; 231 } 232 } 233 int iarg = optind; 234 235 struct Table section_infos; 236 table_init(§ion_infos); 237 struct Table label_table; 238 table_init(&label_table); 239 240 struct ParseInfo info; 241 info.error_count = 0; 242 info.section_infos = §ion_infos; 243 info.label_table = &label_table; 244 245 if (iarg >= argc) 246 error("No input files"); 247 248 for (int i = iarg; i < argc; ++i) { 249 const char *filename = argv[i]; 250 FILE *fp; 251 if (strcmp(filename, "-") == 0) { 252 fp = stdin; 253 } else if (!is_file(filename) || (fp = fopen(filename, "r")) == NULL) { 254 error("Cannot open %s\n", argv[i]); 255 } 256 257 info.filename = filename; 258 parse_file(fp, &info); 259 fclose(fp); 260 if (info.error_count != 0) 261 break; 262 } 263 264 if (info.error_count != 0) { 265 return 1; 266 } 267 268 struct Vector *sections = sort_sections(§ion_infos); 269 struct Vector *unresolved = new_vector(); 270 int settle1, settle2; 271 do { 272 settle1 = calc_label_address(LOAD_ADDRESS, sections, &label_table); 273 settle2 = resolve_relative_address(sections, &label_table, unresolved); 274 } while (!(settle1 && settle2)); 275 276 for (int i = 0; i < unresolved->len; ++i) { 277 struct UnresolvedInfo *u = unresolved->data[i]; 278 make_label_referred(&label_table, u->label, 1); 279 } 280 281 emit_irs(sections); 282 283 fix_section_size(sections, LOAD_ADDRESS); 284 285 int result = EMIT_OBJ(ofn, sections, &label_table, unresolved); 286 if (result != 0) { 287 if (ofn == NULL && !isatty(STDIN_FILENO)) 288 drop_all(stdin); 289 } 290 return result; 291 }