Repository navigation
Expand file tree
/
Copy pathutilities.cpp
More file actions
147 lines (122 loc) · 3.92 KB
/
Copy pathutilities.cpp
File metadata and controls
147 lines (122 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/**
* @file utilities.cpp
* @brief Utility functions for file loading, tokenization, and relative anonymous label resolution.
* @author Paul Baxter
*/
#include "utilities.h"
#include <chrono>
#include <exception>
#include <format>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stack>
#include <utility>
#include "AssemblerParser.h"
#include "PasmTokenizer.hpp"
#include "anonymouslabel.h"
#include "getmangledsymbol.h"
#include "macrodef.h"
#include "multipassassembler.h"
#include "opcodedict.h"
#include "opcodeinfo.h"
#include "sourceManager.h"
#include "symboltable.h"
std::vector<PasmTokenizer::Token> LoadAndTokenizeFile(
const std::string& filepath,
SourceManager& src_mgr,
const PasmTokenizer& tokenizer
) {
src_mgr.PushInclude(filepath);
struct IncludeGuard {
SourceManager& mgr;
~IncludeGuard() { mgr.PopInclude(); }
} guard{src_mgr};
int fileid = src_mgr.GetOrRegisterFile(filepath);
std::ifstream infile(filepath);
if (!infile.is_open()) {
std::cerr << std::format("Error: Unable to open file {}\n", filepath);
return {};
}
int lineNo = 1;
std::string line;
std::string source_code;
while (std::getline(infile, line)) {
src_mgr.source[{fileid, lineNo}] = line;
source_code.append(line).append("\n");
lineNo++;
}
return tokenizer.tokenize(source_code, fileid);
}
std::optional<int> FindAnonLabel(
const std::vector<AnonymousLabel>& anonymous_labels,
bool forward,
int count,
uint16_t pc
) {
if (anonymous_labels.empty()) {
return std::nullopt;
}
// Binary search to find boundary around 'pc'
size_t lo = 0;
size_t hi = anonymous_labels.size();
while (lo < hi) {
size_t mid = lo + (hi - lo) / 2;
if (anonymous_labels[mid].address <= pc) {
lo = mid + 1;
} else {
hi = mid;
}
}
size_t start_index = lo;
int found_count = 0;
if (forward) {
for (size_t i = start_index; i < anonymous_labels.size(); ++i) {
if (anonymous_labels[i].type == '+') {
if (++found_count == count) {
return anonymous_labels[i].address;
}
}
}
} else {
if (start_index == 0) {
return std::nullopt;
}
// Loop downward safely using unsigned indices
for (size_t i = start_index; i > 0; --i) {
size_t back_index = i - 1;
if (anonymous_labels[back_index].type == '-') {
if (++found_count == count) {
return anonymous_labels[back_index].address;
}
}
}
}
return std::nullopt;
}
std::vector<uint8_t> read_binary_file_slice(const std::string& filepath, size_t offset, size_t length)
{
std::ifstream file(filepath, std::ios::binary | std::ios::ate);
if (!file.is_open()) {
throw std::runtime_error("Unable to open file '" + filepath + "'");
}
size_t total_size = static_cast<size_t>(file.tellg());
if (offset >= total_size) {
throw std::runtime_error("Offset exceeds file size");
}
// Calculate actual bytes to read
size_t bytes_to_read = total_size - offset;
if (length > 0 && length > bytes_to_read) {
throw std::runtime_error("Invalid number of bytes to read.");
}
if (length > 0 && length < bytes_to_read) {
bytes_to_read = length;
}
std::vector<uint8_t> buffer(bytes_to_read);
file.seekg(static_cast<std::streamoff>(offset), std::ios::beg);
if (!file.read(reinterpret_cast<char*>(buffer.data()), static_cast<std::streamsize>(bytes_to_read))) {
throw std::runtime_error("Failed reading stream bytes");
}
return buffer;
}