| ... | @@ -1,14 +1,15 @@ | ... | @@ -1,14 +1,15 @@ |
| 1 | path: Path, | 1 | path: Path, |
| 2 | cpu_arch: ?std.Target.Cpu.Arch = null, | 2 | cpu_arch: ?std.Target.Cpu.Arch, |
| 3 | args: std.ArrayListUnmanaged(Arg) = .empty, | 3 | args: []const Arg, |
| 4 | | 4 | |
| 5 | pub const Arg = struct { | 5 | pub const Arg = struct { |
| 6 | needed: bool = false, | 6 | needed: bool = false, |
| 7 | path: []const u8, | 7 | path: []const u8, |
| 8 | }; | 8 | }; |
| 9 | | 9 | |
| 10 | pub fn deinit(scr: *LdScript, allocator: Allocator) void { | 10 | pub fn deinit(ls: *LdScript, gpa: Allocator) void { |
| 11 | scr.args.deinit(allocator); | 11 | gpa.free(ls.args); |
| | 12 | ls.* = undefined; |
| 12 | } | 13 | } |
| 13 | | 14 | |
| 14 | pub const Error = error{ | 15 | pub const Error = error{ |
| ... | @@ -18,28 +19,30 @@ pub const Error = error{ | ... | @@ -18,28 +19,30 @@ pub const Error = error{ |
| 18 | OutOfMemory, | 19 | OutOfMemory, |
| 19 | }; | 20 | }; |
| 20 | | 21 | |
| 21 | pub fn parse(scr: *LdScript, data: []const u8, elf_file: *Elf) Error!void { | 22 | pub fn parse( |
| 22 | const comp = elf_file.base.comp; | 23 | gpa: Allocator, |
| 23 | const gpa = comp.gpa; | 24 | diags: *Diags, |
| 24 | const diags = &comp.link_diags; | 25 | /// For error reporting. |
| 25 | | 26 | path: Path, |
| | 27 | data: []const u8, |
| | 28 | ) Error!LdScript { |
| 26 | var tokenizer = Tokenizer{ .source = data }; | 29 | var tokenizer = Tokenizer{ .source = data }; |
| 27 | var tokens = std.ArrayList(Token).init(gpa); | 30 | var tokens: std.ArrayListUnmanaged(Token) = .empty; |
| 28 | defer tokens.deinit(); | 31 | defer tokens.deinit(gpa); |
| 29 | var line_col = std.ArrayList(LineColumn).init(gpa); | 32 | var line_col: std.ArrayListUnmanaged(LineColumn) = .empty; |
| 30 | defer line_col.deinit(); | 33 | defer line_col.deinit(gpa); |
| 31 | | 34 | |
| 32 | var line: usize = 0; | 35 | var line: usize = 0; |
| 33 | var prev_line_last_col: usize = 0; | 36 | var prev_line_last_col: usize = 0; |
| 34 | | 37 | |
| 35 | while (true) { | 38 | while (true) { |
| 36 | const tok = tokenizer.next(); | 39 | const tok = tokenizer.next(); |
| 37 | try tokens.append(tok); | 40 | try tokens.append(gpa, tok); |
| 38 | const column = tok.start - prev_line_last_col; | 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 | switch (tok.id) { | 43 | switch (tok.id) { |
| 41 | .invalid => { | 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 | std.fmt.fmtSliceEscapeLower(tok.get(data)), line, column, | 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,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 }; | 58 | var it: TokenIterator = .{ .tokens = tokens.items }; |
| 56 | var parser = Parser{ .source = data, .it = &it }; | 59 | var parser: Parser = .{ |
| 57 | var args = std.ArrayList(Arg).init(gpa); | 60 | .gpa = gpa, |
| 58 | scr.doParse(.{ | 61 | .source = data, |
| 59 | .parser = &parser, | 62 | .it = &it, |
| 60 | .args = &args, | 63 | .args = .empty, |
| 61 | }) catch |err| switch (err) { | 64 | .cpu_arch = null, |
| | 65 | }; |
| | 66 | defer parser.args.deinit(gpa); |
| | 67 | |
| | 68 | parser.start() catch |err| switch (err) { |
| 62 | error.UnexpectedToken => { | 69 | error.UnexpectedToken => { |
| 63 | const last_token_id = parser.it.pos - 1; | 70 | const last_token_id = parser.it.pos - 1; |
| 64 | const last_token = parser.it.get(last_token_id); | 71 | const last_token = parser.it.get(last_token_id); |
| 65 | const lcol = line_col.items[last_token_id]; | 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 | @tagName(last_token.id), | 74 | @tagName(last_token.id), |
| 68 | last_token.get(data), | 75 | last_token.get(data), |
| 69 | lcol.line, | 76 | lcol.line, |
| ... | @@ -72,30 +79,10 @@ pub fn parse(scr: *LdScript, data: []const u8, elf_file: *Elf) Error!void { | ... | @@ -72,30 +79,10 @@ pub fn parse(scr: *LdScript, data: []const u8, elf_file: *Elf) Error!void { |
| 72 | }, | 79 | }, |
| 73 | else => |e| return e, | 80 | else => |e| return e, |
| 74 | }; | 81 | }; |
| 75 | scr.args = args.moveToUnmanaged(); | 82 | return .{ |
| 76 | } | 83 | .path = path, |
| 77 | | 84 | .cpu_arch = parser.cpu_arch, |
| 78 | fn doParse(scr: *LdScript, ctx: struct { | 85 | .args = try parser.args.toOwnedSlice(gpa), |
| 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, | | |
| 99 | }; | 86 | }; |
| 100 | } | 87 | } |
| 101 | | 88 | |
| ... | @@ -126,9 +113,34 @@ const Command = enum { | ... | @@ -126,9 +113,34 @@ const Command = enum { |
| 126 | }; | 113 | }; |
| 127 | | 114 | |
| 128 | const Parser = struct { | 115 | const Parser = struct { |
| | 116 | gpa: Allocator, |
| 129 | source: []const u8, | 117 | source: []const u8, |
| 130 | it: *TokenIterator, | 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 | fn outputFormat(p: *Parser) !std.Target.Cpu.Arch { | 144 | fn outputFormat(p: *Parser) !std.Target.Cpu.Arch { |
| 133 | const value = value: { | 145 | const value = value: { |
| 134 | if (p.skip(&.{.lparen})) { | 146 | if (p.skip(&.{.lparen})) { |
| ... | @@ -149,18 +161,19 @@ const Parser = struct { | ... | @@ -149,18 +161,19 @@ const Parser = struct { |
| 149 | return error.UnknownCpuArch; | 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 | if (!p.skip(&.{.lparen})) return error.UnexpectedToken; | 166 | if (!p.skip(&.{.lparen})) return error.UnexpectedToken; |
| 154 | | 167 | |
| 155 | while (true) { | 168 | while (true) { |
| 156 | if (p.maybe(.literal)) |tok_id| { | 169 | if (p.maybe(.literal)) |tok_id| { |
| 157 | const tok = p.it.get(tok_id); | 170 | const tok = p.it.get(tok_id); |
| 158 | const path = tok.get(p.source); | 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 | } else if (p.maybe(.command)) |cmd_id| { | 173 | } else if (p.maybe(.command)) |cmd_id| { |
| 161 | const cmd = p.getCommand(cmd_id); | 174 | const cmd = p.getCommand(cmd_id); |
| 162 | switch (cmd) { | 175 | switch (cmd) { |
| 163 | .as_needed => try p.asNeeded(args), | 176 | .as_needed => try p.asNeeded(), |
| 164 | else => return error.UnexpectedToken, | 177 | else => return error.UnexpectedToken, |
| 165 | } | 178 | } |
| 166 | } else break; | 179 | } else break; |
| ... | @@ -169,13 +182,14 @@ const Parser = struct { | ... | @@ -169,13 +182,14 @@ const Parser = struct { |
| 169 | _ = try p.require(.rparen); | 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 | if (!p.skip(&.{.lparen})) return error.UnexpectedToken; | 187 | if (!p.skip(&.{.lparen})) return error.UnexpectedToken; |
| 174 | | 188 | |
| 175 | while (p.maybe(.literal)) |tok_id| { | 189 | while (p.maybe(.literal)) |tok_id| { |
| 176 | const tok = p.it.get(tok_id); | 190 | const tok = p.it.get(tok_id); |
| 177 | const path = tok.get(p.source); | 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 | _ = try p.require(.rparen); | 195 | _ = try p.require(.rparen); |
| ... | @@ -227,21 +241,19 @@ const Token = struct { | ... | @@ -227,21 +241,19 @@ const Token = struct { |
| 227 | end: usize, | 241 | end: usize, |
| 228 | | 242 | |
| 229 | const Id = enum { | 243 | const Id = enum { |
| 230 | // zig fmt: off | | |
| 231 | eof, | 244 | eof, |
| 232 | invalid, | 245 | invalid, |
| 233 | | 246 | |
| 234 | new_line, | 247 | new_line, |
| 235 | lparen, // ( | 248 | lparen, // ( |
| 236 | rparen, // ) | 249 | rparen, // ) |
| 237 | lbrace, // { | 250 | lbrace, // { |
| 238 | rbrace, // } | 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 | literal, | 256 | literal, |
| 244 | // zig fmt: on | | |
| 245 | }; | 257 | }; |
| 246 | | 258 | |
| 247 | const Index = usize; | 259 | const Index = usize; |
| ... | @@ -430,10 +442,9 @@ const TokenIterator = struct { | ... | @@ -430,10 +442,9 @@ const TokenIterator = struct { |
| 430 | }; | 442 | }; |
| 431 | | 443 | |
| 432 | const LdScript = @This(); | 444 | const LdScript = @This(); |
| | 445 | const Diags = @import("../../link.zig").Diags; |
| 433 | | 446 | |
| 434 | const std = @import("std"); | 447 | const std = @import("std"); |
| 435 | const assert = std.debug.assert; | 448 | const assert = std.debug.assert; |
| 436 | const Path = std.Build.Cache.Path; | 449 | const Path = std.Build.Cache.Path; |
| 437 | | | |
| 438 | const Allocator = std.mem.Allocator; | 450 | const Allocator = std.mem.Allocator; |
| 439 | const Elf = @import("../Elf.zig"); | | |