1 |
9 |
ring0_mipt |
/*
|
2 |
|
|
* Copyright (c) 2016 by Alex I. Kuznetsov.
|
3 |
|
|
*
|
4 |
|
|
* Part of the LXP32 CPU IP core.
|
5 |
|
|
*
|
6 |
|
|
* This module defines the Assembler class which performs
|
7 |
|
|
* compilation of LXP32 assembly source files.
|
8 |
|
|
*/
|
9 |
|
|
|
10 |
|
|
#ifndef ASSEMBLER_H_INCLUDED
|
11 |
|
|
#define ASSEMBLER_H_INCLUDED
|
12 |
|
|
|
13 |
|
|
#include "linkableobject.h"
|
14 |
|
|
|
15 |
|
|
#include <vector>
|
16 |
|
|
#include <map>
|
17 |
|
|
#include <string>
|
18 |
|
|
#include <cstdint>
|
19 |
|
|
|
20 |
|
|
class Assembler {
|
21 |
|
|
typedef std::vector<std::string> TokenList;
|
22 |
|
|
typedef std::int_least64_t Integer;
|
23 |
|
|
enum LexerState {
|
24 |
|
|
Initial,
|
25 |
|
|
Word,
|
26 |
|
|
StringLiteral,
|
27 |
|
|
BlockComment
|
28 |
|
|
};
|
29 |
|
|
struct Operand {
|
30 |
|
|
enum Type {Null,Register,Identifier,NumericLiteral};
|
31 |
|
|
Type type=Null;
|
32 |
|
|
std::string str;
|
33 |
|
|
Integer i=0;
|
34 |
|
|
std::uint8_t reg=0;
|
35 |
|
|
};
|
36 |
|
|
|
37 |
|
|
LinkableObject _obj;
|
38 |
|
|
std::map<std::string,TokenList> _macros;
|
39 |
|
|
LexerState _state;
|
40 |
|
|
int _line;
|
41 |
|
|
std::vector<std::string> _currentLabels;
|
42 |
|
|
std::string _currentFileName;
|
43 |
|
|
std::vector<std::string> _includeSearchDirs;
|
44 |
|
|
std::vector<std::string> _exportedSymbols;
|
45 |
|
|
std::vector<bool> _sectionEnabled;
|
46 |
|
|
public:
|
47 |
|
|
void processFile(const std::string &filename);
|
48 |
|
|
|
49 |
|
|
void addIncludeSearchDir(const std::string &dir);
|
50 |
|
|
|
51 |
|
|
int line() const;
|
52 |
|
|
std::string currentFileName() const;
|
53 |
|
|
|
54 |
|
|
LinkableObject &object();
|
55 |
|
|
const LinkableObject &object() const;
|
56 |
|
|
private:
|
57 |
|
|
void processFileRecursive(const std::string &filename);
|
58 |
|
|
TokenList tokenize(const std::string &str);
|
59 |
|
|
void expand(TokenList &list);
|
60 |
|
|
void elaborate(TokenList &list);
|
61 |
|
|
|
62 |
|
|
void elaborateDirective(TokenList &list);
|
63 |
|
|
LinkableObject::Word elaborateDataDefinition(TokenList &list);
|
64 |
|
|
LinkableObject::Word elaborateInstruction(TokenList &list);
|
65 |
|
|
|
66 |
|
|
bool isSectionEnabled() const;
|
67 |
|
|
static bool validateIdentifier(const std::string &str);
|
68 |
|
|
static Integer numericLiteral(const std::string &str);
|
69 |
|
|
static std::vector<Operand> getOperands(const TokenList &list);
|
70 |
|
|
|
71 |
|
|
// LXP32 instructions
|
72 |
|
|
void encodeDstOperand(LinkableObject::Word &word,const Operand &arg);
|
73 |
|
|
void encodeRd1Operand(LinkableObject::Word &word,const Operand &arg);
|
74 |
|
|
void encodeRd2Operand(LinkableObject::Word &word,const Operand &arg);
|
75 |
|
|
|
76 |
|
|
void encodeAdd(const TokenList &list);
|
77 |
|
|
void encodeAnd(const TokenList &list);
|
78 |
|
|
void encodeCall(const TokenList &list);
|
79 |
|
|
void encodeCjmpxx(const TokenList &list);
|
80 |
|
|
void encodeDivs(const TokenList &list);
|
81 |
|
|
void encodeDivu(const TokenList &list);
|
82 |
|
|
void encodeHlt(const TokenList &list);
|
83 |
|
|
void encodeJmp(const TokenList &list);
|
84 |
|
|
void encodeIret(const TokenList &list);
|
85 |
|
|
void encodeLc(const TokenList &list);
|
86 |
|
|
void encodeLcs(const TokenList &list);
|
87 |
|
|
void encodeLsb(const TokenList &list);
|
88 |
|
|
void encodeLub(const TokenList &list);
|
89 |
|
|
void encodeLw(const TokenList &list);
|
90 |
|
|
void encodeMods(const TokenList &list);
|
91 |
|
|
void encodeModu(const TokenList &list);
|
92 |
|
|
void encodeMov(const TokenList &list);
|
93 |
|
|
void encodeMul(const TokenList &list);
|
94 |
|
|
void encodeNeg(const TokenList &list);
|
95 |
|
|
void encodeNop(const TokenList &list);
|
96 |
|
|
void encodeNot(const TokenList &list);
|
97 |
|
|
void encodeOr(const TokenList &list);
|
98 |
|
|
void encodeRet(const TokenList &list);
|
99 |
|
|
void encodeSb(const TokenList &list);
|
100 |
|
|
void encodeSl(const TokenList &list);
|
101 |
|
|
void encodeSrs(const TokenList &list);
|
102 |
|
|
void encodeSru(const TokenList &list);
|
103 |
|
|
void encodeSub(const TokenList &list);
|
104 |
|
|
void encodeSw(const TokenList &list);
|
105 |
|
|
void encodeXor(const TokenList &list);
|
106 |
|
|
};
|
107 |
|
|
|
108 |
|
|
#endif
|