-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAssembler.h
138 lines (101 loc) · 4.12 KB
/
Assembler.h
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
//
// Created by Lars Ott on 8/24/17.
//
#include <iostream>
#include <fstream>
#include <regex>
#include <map>
#include "Utility.h"
#include "GlobalTypes.h"
#include "computer.h"
#include <QString>
#ifndef LC3_ASSAMBLER_ASSAMBLER_H
#define LC3_ASSAMBLER_ASSAMBLER_H
typedef enum {
numberOnly,
labelOnly,
numberOrLabel,
stringOnly,
none
} NumOrLabel;
typedef enum {
PreRun,
MainRun
} RunType;
class Assembler {
private:
const std::regex parserRegex;
std::map<QString, uint16_t> labelDict;
std::map<mem_addr_t, QString> commentDict;
std::map<mem_addr_t, data_t> dataDict;
/** used to annotate comments from lines that do not have instructions
* \brief annotatedComment
*/
QString annotatedComment;
/** How many lines the program has.
* /brief programLength
*/
uint16_t programLength;
/** starting address of the program being assembled
*/
val_t startingAddress;
/** ending address of the program being assembled
*/
val_t endingAddress;
/** used during assembly runs. set to true when .END is encountered
* \brief hitDotEND
*/
bool hitDotEND;
double parseNumber(std::string num);
std::string unescapeString(std::string unescaped);
void writeWord(std::ostream &stream, uint16_t value);
void determineArgumentsOfOp(const std::string &instruction, bool thirdRegEmpty, int &nOfRegs, NumOrLabel &nOrL);
uint16_t OpCodeForInstruction(std::string &op);
uint16_t getConstantBits(const std::string &instruction, int nOfRegs, NumOrLabel nOrL);
uint16_t getNumberOrOffset(const std::string &instruction, NumOrLabel nOrL, const std::string &opNumber, const std::string &opLabel,
uint16_t pc);
std::string checkInput(const std::string &instruction, const std::string &firstReg, const std::string &secondReg, const std::string &thirdReg,
const std::string &opNumber, const std::string &opLabel, const std::string &opString, int nOfRegs, NumOrLabel nOrL);
uint16_t getRegisters(std::string &instruction, int nOfRegs, std::string &firstReg, std::string &secondReg, std::string &thirdReg);
void writeFill(std::ofstream &oStream, const std::string &opNumber, const std::string &opLabel);
// returns new pc
uint16_t writeInstruction(RunType runType, std::string &instruction, std::string &firstReg, std::string &secondReg, std::string &thirdReg,
std::string &opNumber, std::string &opLabel, std::string &opString,
int nOfRegs, NumOrLabel nOrL,
std::ofstream &oStream, uint16_t pc);
// returns new pc
uint16_t processLine(std::string &line, RunType runType, uint16_t pc, std::ofstream &oStream);
data_t getNumberType(std::string num);
void cleanComments();
void assembleFileButForSomeReasonTheOtherOneDoesntWork(const char *infile, const char *outFile);
public:
Assembler();
void assembleFile(const char *inFile, const char *outFile);
std::map<QString, uint16_t>* labelDictCopy();
/** Returns all the labels for an address as a space separated string
* /brief labelForAddress
* /param addr
* /return
*/
QString labelForAddress(mem_addr_t addr);
/** Passes all labels from the assambled file to a computer. Replaces all existing labels in the range of the assembled file
* /brief passLabelsToComputer
* /param comp
*/
void passLabelsToComputer(Computer* comp);
/** Returns all the comments for an address as a space separated string
* /brief commentForAddress
* /param addr
* /return
*/
QString commentForAddress(mem_addr_t addr);
/** Passes all comments from the assambled file to a computer. Replaces all existing labels in the range of the assembled file
* /brief passCommentsToComputer
* /param comp
*/
void passCommentsToComputer(Computer* comp);
data_t dataTypeForAddress(mem_addr_t addr);
void passDataTypesToComputer(Computer* comp);
val_t singleLineAssemble(QString machineCode,mem_addr_t addr);
};
#endif //LC3_ASSAMBLER_ASSAMBLER_H