-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutilities.cpp
More file actions
118 lines (97 loc) · 3.04 KB
/
Copy pathutilities.cpp
File metadata and controls
118 lines (97 loc) · 3.04 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
#include <iomanip>
#include <sstream>
#include <utility>
#include <format>
#include <fstream>
#include <sstream>
#include <exception>
#include <stack>
#include <chrono>
#include <iostream>
#include "opcodedict.h"
#include "PasmTokenizer.hpp"
#include "anonymouslabel.h"
#include "multipassassembler.h"
#include "symboltable.h"
#include "sourceManager.h"
#include "getmangledsymbol.h"
#include "opcodeinfo.h"
#include "exprNode.h"
#include "macrodef.h"
#include "utilities.h"
#include "AssemblerParser.h"
#include "utilities.h"
std::vector<PasmTokenizer::Token> LoadAndTokenizeFile(
const std::string& filepath,
SourceManager& src_mgr,
const PasmTokenizer& tokenizer
) {
src_mgr.PushInclude(filepath);
int fileid = src_mgr.GetOrRegisterFile(filepath);
int lineNo = 1;
std::string line;
std::string source_code;
std::ifstream infile(filepath);
while (std::getline(infile, line)) {
src_mgr.source[{fileid, lineNo}] = line;
source_code += (line + "\n");
lineNo++;
}
infile.close();
auto filetokens = tokenizer.tokenize(source_code, fileid);
src_mgr.PopInclude();
return filetokens;
}
std::optional<int> FindAnonLabel(const std::vector<AnonymousLabel>& anonymous_labels, bool forward, int count, uint16_t pc) {
auto sz = anonymous_labels.size();
if (sz == 0) {
return std::nullopt;
}
// 1. Binary search to find the first label strictly AFTER the current pc.
size_t lo = 0;
size_t hi = sz;
while (lo < hi) {
size_t mid = lo + (hi - lo) / 2; // Corrected midpoint calculation
if (anonymous_labels[mid].address <= pc) {
lo = mid + 1;
} else {
hi = mid;
}
}
// 'lo' is now the index of the first anonymous label after the current PC.
size_t start_index = lo;
int found_count = 0;
// 2. Scan in the requested direction
if (forward) {
// Searching forward: start from 'start_index' and scan to the end
while (start_index < sz) {
auto& lbl = anonymous_labels[start_index];
if (lbl.type == '+') {
found_count++;
if (found_count == count) {
return lbl.address;
}
}
start_index++; // Moved outside the if-statement to prevent infinite loops!
}
}
else {
// Searching backward: start from 'start_index - 1' and scan down to 0
if (start_index == 0) {
return std::nullopt; // No labels exist before the PC
}
size_t back_index = start_index - 1;
while (true) {
auto& lbl = anonymous_labels[back_index];
if (lbl.type == '-') {
found_count++;
if (found_count == count) {
return lbl.address;
}
}
if (back_index == 0) break; // Reached the beginning
back_index--;
}
}
return std::nullopt;
}