| ... | ... | @@ -1,14 +1,15 @@ |
| 1 | 1 | path: Path, |
| 2 | | cpu_arch: ?std.Target.Cpu.Arch = null, |
| 3 | | args: std.ArrayListUnmanaged(Arg) = .empty, |
| 2 | cpu_arch: ?std.Target.Cpu.Arch, |
| 3 | args: []const Arg, |
| 4 | 4 | |
| 5 | 5 | pub const Arg = struct { |
| 6 | 6 | needed: bool = false, |
| 7 | 7 | path: []const u8, |
| 8 | 8 | }; |
| 9 | 9 | |
| 10 | | pub fn deinit(scr: *LdScript, allocator: Allocator) void { |
| 11 | | scr.args.deinit(allocator); |
| 10 | pub fn deinit(ls: *LdScript, gpa: Allocator) void { |
| 11 | gpa.free(ls.args); |
| 12 | ls.* = undefined; |
| 12 | 13 | } |
| 13 | 14 | |
| 14 | 15 | pub const Error = error{ |
| ... | ... | @@ -18,28 +19,30 @@ pub const Error = error{ |
| 18 | 19 | OutOfMemory, |
| 19 | 20 | }; |
| 20 | 21 | |
| 21 | | pub fn parse(scr: *LdScript, data: []const u8, elf_file: *Elf) Error!void { |
| 22 | | const comp = elf_file.base.comp; |
| 23 | | const gpa = comp.gpa; |
| 24 | | const diags = &comp.link_diags; |
| 25 | | |
| 22 | pub fn parse( |
| 23 | gpa: Allocator, |
| 24 | diags: *Diags, |
| 25 | /// For error reporting. |
| 26 | path: Path, |
| 27 | data: []const u8, |
| 28 | ) Error!LdScript { |
| 26 | 29 | var tokenizer = Tokenizer{ .source = data }; |
| 27 | | var tokens = std.ArrayList(Token).init(gpa); |
| 28 | | defer tokens.deinit(); |
| 29 | | var line_col = std.ArrayList(LineColumn).init(gpa); |
| 30 | | defer line_col.deinit(); |
| 30 | var tokens: std.ArrayListUnmanaged(Token) = .empty; |
| 31 | defer tokens.deinit(gpa); |
| 32 | var line_col: std.ArrayListUnmanaged(LineColumn) = .empty; |
| 33 | defer line_col.deinit(gpa); |
| 31 | 34 | |
| 32 | 35 | var line: usize = 0; |
| 33 | 36 | var prev_line_last_col: usize = 0; |
| 34 | 37 | |
| 35 | 38 | while (true) { |
| 36 | 39 | const tok = tokenizer.next(); |
| 37 | | try tokens.append(tok); |
| 40 | try tokens.append(gpa, tok); |
| 38 | 41 | const column = tok.start - prev_line_last_col; |
| 39 | | try line_col.append(.{ .line = line, .column = column }); |
| 42 | try line_col.append(gpa, .{ .line = line, .column = column }); |
| 40 | 43 | switch (tok.id) { |
| 41 | 44 | .invalid => { |
| 42 | | return diags.failParse(scr.path, "invalid token in LD script: '{s}' ({d}:{d})", .{ |
| 45 | return diags.failParse(path, "invalid token in LD script: '{s}' ({d}:{d})", .{ |
| 43 | 46 | std.fmt.fmtSliceEscapeLower(tok.get(data)), line, column, |
| 44 | 47 | }); |
| 45 | 48 | }, |
| ... | ... | @@ -52,18 +55,22 @@ pub fn parse(scr: *LdScript, data: []const u8, elf_file: *Elf) Error!void { |
| 52 | 55 | } |
| 53 | 56 | } |
| 54 | 57 | |
| 55 | | var it = TokenIterator{ .tokens = tokens.items }; |
| 56 | | var parser = Parser{ .source = data, .it = &it }; |
| 57 | | var args = std.ArrayList(Arg).init(gpa); |
| 58 | | scr.doParse(.{ |
| 59 | | .parser = &parser, |
| 60 | | .args = &args, |
| 61 | | }) catch |err| switch (err) { |
| 58 | var it: TokenIterator = .{ .tokens = tokens.items }; |
| 59 | var parser: Parser = .{ |
| 60 | .gpa = gpa, |
| 61 | .source = data, |
| 62 | .it = &it, |
| 63 | .args = .empty, |
| 64 | .cpu_arch = null, |
| 65 | }; |
| 66 | defer parser.args.deinit(gpa); |
| 67 | |
| 68 | parser.start() catch |err| switch (err) { |
| 62 | 69 | error.UnexpectedToken => { |
| 63 | 70 | const last_token_id = parser.it.pos - 1; |
| 64 | 71 | const last_token = parser.it.get(last_token_id); |
| 65 | 72 | const lcol = line_col.items[last_token_id]; |
| 66 | | return diags.failParse(scr.path, "unexpected token in LD script: {s}: '{s}' ({d}:{d})", .{ |
| 73 | return diags.failParse(path, "unexpected token in LD script: {s}: '{s}' ({d}:{d})", .{ |
| 67 | 74 | @tagName(last_token.id), |
| 68 | 75 | last_token.get(data), |
| 69 | 76 | lcol.line, |
| ... | ... | @@ -72,30 +79,10 @@ pub fn parse(scr: *LdScript, data: []const u8, elf_file: *Elf) Error!void { |
| 72 | 79 | }, |
| 73 | 80 | else => |e| return e, |
| 74 | 81 | }; |
| 75 | | scr.args = args.moveToUnmanaged(); |
| 76 | | } |
| 77 | | |
| 78 | | fn doParse(scr: *LdScript, ctx: struct { |
| 79 | | parser: *Parser, |
| 80 | | args: *std.ArrayList(Arg), |
| 81 | | }) !void { |
| 82 | | while (true) { |
| 83 | | ctx.parser.skipAny(&.{ .comment, .new_line }); |
| 84 | | |
| 85 | | if (ctx.parser.maybe(.command)) |cmd_id| { |
| 86 | | const cmd = ctx.parser.getCommand(cmd_id); |
| 87 | | switch (cmd) { |
| 88 | | .output_format => scr.cpu_arch = try ctx.parser.outputFormat(), |
| 89 | | // TODO we should verify that group only contains libraries |
| 90 | | .input, .group => try ctx.parser.group(ctx.args), |
| 91 | | else => return error.UnexpectedToken, |
| 92 | | } |
| 93 | | } else break; |
| 94 | | } |
| 95 | | |
| 96 | | if (ctx.parser.it.next()) |tok| switch (tok.id) { |
| 97 | | .eof => {}, |
| 98 | | else => return error.UnexpectedToken, |
| 82 | return .{ |
| 83 | .path = path, |
| 84 | .cpu_arch = parser.cpu_arch, |
| 85 | .args = try parser.args.toOwnedSlice(gpa), |
| 99 | 86 | }; |
| 100 | 87 | } |
| 101 | 88 | |
| ... | ... | @@ -126,9 +113,34 @@ const Command = enum { |
| 126 | 113 | }; |
| 127 | 114 | |
| 128 | 115 | const Parser = struct { |
| 116 | gpa: Allocator, |
| 129 | 117 | source: []const u8, |
| 130 | 118 | it: *TokenIterator, |
| 131 | 119 | |
| 120 | cpu_arch: ?std.Target.Cpu.Arch, |
| 121 | args: std.ArrayListUnmanaged(Arg), |
| 122 | |
| 123 | fn start(parser: *Parser) !void { |
| 124 | while (true) { |
| 125 | parser.skipAny(&.{ .comment, .new_line }); |
| 126 | |
| 127 | if (parser.maybe(.command)) |cmd_id| { |
| 128 | const cmd = parser.getCommand(cmd_id); |
| 129 | switch (cmd) { |
| 130 | .output_format => parser.cpu_arch = try parser.outputFormat(), |
| 131 | // TODO we should verify that group only contains libraries |
| 132 | .input, .group => try parser.group(), |
| 133 | else => return error.UnexpectedToken, |
| 134 | } |
| 135 | } else break; |
| 136 | } |
| 137 | |
| 138 | if (parser.it.next()) |tok| switch (tok.id) { |
| 139 | .eof => {}, |
| 140 | else => return error.UnexpectedToken, |
| 141 | }; |
| 142 | } |
| 143 | |
| 132 | 144 | fn outputFormat(p: *Parser) !std.Target.Cpu.Arch { |
| 133 | 145 | const value = value: { |
| 134 | 146 | if (p.skip(&.{.lparen})) { |
| ... | ... | @@ -149,18 +161,19 @@ const Parser = struct { |
| 149 | 161 | return error.UnknownCpuArch; |
| 150 | 162 | } |
| 151 | 163 | |
| 152 | | fn group(p: *Parser, args: *std.ArrayList(Arg)) !void { |
| 164 | fn group(p: *Parser) !void { |
| 165 | const gpa = p.gpa; |
| 153 | 166 | if (!p.skip(&.{.lparen})) return error.UnexpectedToken; |
| 154 | 167 | |
| 155 | 168 | while (true) { |
| 156 | 169 | if (p.maybe(.literal)) |tok_id| { |
| 157 | 170 | const tok = p.it.get(tok_id); |
| 158 | 171 | const path = tok.get(p.source); |
| 159 | | try args.append(.{ .path = path, .needed = true }); |
| 172 | try p.args.append(gpa, .{ .path = path, .needed = true }); |
| 160 | 173 | } else if (p.maybe(.command)) |cmd_id| { |
| 161 | 174 | const cmd = p.getCommand(cmd_id); |
| 162 | 175 | switch (cmd) { |
| 163 | | .as_needed => try p.asNeeded(args), |
| 176 | .as_needed => try p.asNeeded(), |
| 164 | 177 | else => return error.UnexpectedToken, |
| 165 | 178 | } |
| 166 | 179 | } else break; |
| ... | ... | @@ -169,13 +182,14 @@ const Parser = struct { |
| 169 | 182 | _ = try p.require(.rparen); |
| 170 | 183 | } |
| 171 | 184 | |
| 172 | | fn asNeeded(p: *Parser, args: *std.ArrayList(Arg)) !void { |
| 185 | fn asNeeded(p: *Parser) !void { |
| 186 | const gpa = p.gpa; |
| 173 | 187 | if (!p.skip(&.{.lparen})) return error.UnexpectedToken; |
| 174 | 188 | |
| 175 | 189 | while (p.maybe(.literal)) |tok_id| { |
| 176 | 190 | const tok = p.it.get(tok_id); |
| 177 | 191 | const path = tok.get(p.source); |
| 178 | | try args.append(.{ .path = path, .needed = false }); |
| 192 | try p.args.append(gpa, .{ .path = path, .needed = false }); |
| 179 | 193 | } |
| 180 | 194 | |
| 181 | 195 | _ = try p.require(.rparen); |
| ... | ... | @@ -227,21 +241,19 @@ const Token = struct { |
| 227 | 241 | end: usize, |
| 228 | 242 | |
| 229 | 243 | const Id = enum { |
| 230 | | // zig fmt: off |
| 231 | 244 | eof, |
| 232 | 245 | invalid, |
| 233 | 246 | |
| 234 | 247 | new_line, |
| 235 | | lparen, // ( |
| 236 | | rparen, // ) |
| 237 | | lbrace, // { |
| 238 | | rbrace, // } |
| 248 | lparen, // ( |
| 249 | rparen, // ) |
| 250 | lbrace, // { |
| 251 | rbrace, // } |
| 239 | 252 | |
| 240 | | comment, // /* */ |
| 253 | comment, // /* */ |
| 241 | 254 | |
| 242 | | command, // literal with special meaning, see Command |
| 255 | command, // literal with special meaning, see Command |
| 243 | 256 | literal, |
| 244 | | // zig fmt: on |
| 245 | 257 | }; |
| 246 | 258 | |
| 247 | 259 | const Index = usize; |
| ... | ... | @@ -430,10 +442,9 @@ const TokenIterator = struct { |
| 430 | 442 | }; |
| 431 | 443 | |
| 432 | 444 | const LdScript = @This(); |
| 445 | const Diags = @import("../../link.zig").Diags; |
| 433 | 446 | |
| 434 | 447 | const std = @import("std"); |
| 435 | 448 | const assert = std.debug.assert; |
| 436 | 449 | const Path = std.Build.Cache.Path; |
| 437 | | |
| 438 | 450 | const Allocator = std.mem.Allocator; |
| 439 | | const Elf = @import("../Elf.zig"); |