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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::fmt;
use crate::evm::AbstractInstruction::*;
use crate::evm::{Assembly,AssemblyInstruction,Section};
use crate::util::{FromHexString};
use super::lexer::{Lexer,Token};

// ===================================================================
// Parse Error
// ===================================================================

/// Indicates an error occurred whilst parsing some assembly language
/// into an assembly (i.e. an error originating from the lexer or
/// parser).
#[derive(Debug)]
pub enum AssemblyError {
    ExpectedOperand,
    InvalidComment(usize),
    InvalidHexString(usize),
    InvalidInstruction,
    UnexpectedCharacter(usize),
    UnexpectedToken
}

impl fmt::Display for AssemblyError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:?}", self)
    }
}

impl std::error::Error for AssemblyError {

}

// ===================================================================
// Parser
// ===================================================================

/// Parse assembly language to form an assembly
pub fn parse(input: &str) -> Result<Assembly,AssemblyError> {
    // Holds the set of lines being parsed.
    let mut lexer = Lexer::new(input);
    let mut sections = Vec::new();
    // Keep going until we reach the end.
    while lexer.lookahead()? != Token::EOF {
        sections.push(parse_section(&mut lexer)?);
    }
    // Done
    Ok(Assembly::new(sections))
}

/// Parse a single line of assembly language.
fn parse_section(lexer: &mut Lexer) -> Result<Section<AssemblyInstruction>,AssemblyError> {
    //
    match lexer.next()? {
        Token::Section("code") => {
            parse_code_section(lexer)
        }
        Token::Section("data") => {
            parse_data_section(lexer)
        }
        _ => {
            // Something went wrong
            Err(AssemblyError::UnexpectedToken)
        }
    }
}

fn parse_code_section(lexer: &mut Lexer) -> Result<Section<AssemblyInstruction>,AssemblyError> {
    let mut insns = Vec::new();
    //
    loop {
        match lexer.lookahead()? {
            Token::Identifier("push"|"PUSH") => {
                _ = lexer.next();
                insns.push(parse_push(false,lexer.next()?)?)
            }
            Token::Identifier("pushl"|"PUSHL") => {
                _ = lexer.next();
                insns.push(parse_push(true,lexer.next()?)?)
            }
            Token::Identifier("rjump"|"RJUMP") => {
                _ = lexer.next();
                insns.push(parse_rjump(lexer.next()?)?);
            }
            Token::Identifier("rjumpi"|"RJUMPI") => {
                _ = lexer.next();
                insns.push(parse_rjumpi(lexer.next()?)?);
            }
            Token::Identifier("db"|"DB") => {
                _ = lexer.next();
                insns.push(parse_data(lexer.next()?)?);
            }
            Token::Identifier(id) => {
                _ = lexer.next();
                insns.push(parse_opcode(id)?);
            }
            Token::Label(s) => {
                _ = lexer.next();
                // Mark label in bytecode sequence
                insns.push(LABEL(s.to_string()));
            }
            Token::EOF|Token::Section(_) => {
                return Ok(Section::Code(insns));
            }
            _ => {
                // Something went wrong
                return Err(AssemblyError::UnexpectedToken);
            }
        };
    }
    //
    // FIXME: zeros do not make sense here.

}

fn parse_data_section(lexer: &mut Lexer) -> Result<Section<AssemblyInstruction>,AssemblyError> {
    let mut bytes = Vec::new();
    loop {
        match lexer.lookahead()? {
            Token::Hex(s) => {
                _ = lexer.next();
                bytes.extend(parse_hex(s)?)
            }
            Token::EOF|Token::Section(_) => {
                return Ok(Section::Data(bytes));
            }
            _ => {
                // Something went wrong
                return Err(AssemblyError::UnexpectedToken);
            }
        }
    };
}

/// Parse a push instruction with a given operand.
fn parse_push(large: bool, operand: Token) -> Result<AssemblyInstruction,AssemblyError> {
    // Push always expects an argument, though it could be a
    // label or a hexadecimal operand.
    match operand {
        Token::Hex(s) => Ok(PUSH(parse_hex(s)?)),
        Token::Identifier(s) => Ok(PUSHL(large,s.to_string())),
        Token::EOF => Err(AssemblyError::ExpectedOperand),
        _ => Err(AssemblyError::UnexpectedToken)
    }
}

/// Parse a rjump instruction with a given operand label.
fn parse_rjump(operand: Token) -> Result<AssemblyInstruction,AssemblyError> {
    match operand {
        Token::Identifier(s) => Ok(RJUMP(s.to_string())),
        Token::EOF => Err(AssemblyError::ExpectedOperand),
        _ => Err(AssemblyError::UnexpectedToken)
    }
}

/// Parse a rjumpi instruction with a given operand label.
fn parse_rjumpi(operand: Token) -> Result<AssemblyInstruction,AssemblyError> {
    match operand {
        Token::Identifier(s) => Ok(RJUMPI(s.to_string())),
        Token::EOF => Err(AssemblyError::ExpectedOperand),
        _ => Err(AssemblyError::UnexpectedToken)
    }
}

fn parse_data(operand: Token) -> Result<AssemblyInstruction,AssemblyError> {
    match operand {
        Token::Hex(s) => Ok(DATA(parse_hex(s)?)),
        Token::EOF => Err(AssemblyError::ExpectedOperand),
        _ => Err(AssemblyError::UnexpectedToken)
    }
}

// ===================================================================
// Helpers
// ===================================================================

/// Parse a hexadecimal string
fn parse_hex(hex: &str) -> Result<Vec<u8>,AssemblyError> {
    match hex.from_hex_string() {
        Ok(bytes) => { Ok(bytes) }
        Err(_e) => Err(AssemblyError::InvalidHexString(0))
    }
}

/// Parse a given opcode from a string, and a given number of operand
/// bytes.
fn parse_opcode(insn: &str) -> Result<AssemblyInstruction,AssemblyError> {
    let insn = match insn {
        // 0s: Stop and Arithmetic Operations
        "stop"|"STOP" => STOP,
        "add"|"ADD" => ADD,
        "mul"|"MUL" => MUL,
        "sub"|"SUB" => SUB,
        "div"|"DIV" => DIV,
        "sdiv"|"SDIV" => SDIV,
        "mod"|"MOD" => MOD,
        "smod"|"SMOD" => SMOD,
        "addmod"|"ADDMOD" => ADDMOD,
        "mulmod"|"MULMOD" => MULMOD,
        "exp"|"EXP" => EXP,
        "signextend"|"SIGNEXTEND" => SIGNEXTEND,
        // 10s: Comparison & Bitwise Logic Operations
        "lt"|"LT" => LT,
        "gt"|"GT" => GT,
        "slt"|"SLT" => SLT,
        "sgt"|"SGT" => SGT,
        "eq"|"EQ" => EQ,
        "iszero"|"ISZERO" => ISZERO,
        "and"|"AND" => AND,
        "or"|"OR" => OR,
        "xor"|"XOR" => XOR,
        "not"|"NOT" => NOT,
        "byte"|"BYTE" => BYTE,
        "shl"|"SHL" => SHL,
        "shr"|"SHR" => SHR,
        "sar"|"SAR" => SAR,
        // 20s: Keccak256
        "keccak256"|"KECCAK256" => KECCAK256,
        // 30s: Environmental Information
        "address"|"ADDRESS" => ADDRESS,
        "balance"|"BALANCE" => BALANCE,
        "origin"|"ORIGIN" => ORIGIN,
        "caller"|"CALLER" => CALLER,
        "callvalue"|"CALLVALUE" => CALLVALUE,
        "calldataload"|"CALLDATALOAD" => CALLDATALOAD,
        "calldatasize"|"CALLDATASIZE" => CALLDATASIZE,
        "calldatacopy"|"CALLDATACOPY" => CALLDATACOPY,
        "codesize"|"CODESIZE" => CODESIZE,
        "codecopy"|"CODECOPY" => CODECOPY,
        "gasprice"|"GASPRICE" => GASPRICE,
        "extcodesize"|"EXTCODESIZE" => EXTCODESIZE,
        "extcodecopy"|"EXTCODECOPY" => EXTCODECOPY,
        "returndatasize"|"RETURNDATASIZE" => RETURNDATASIZE,
        "returndatacopy"|"RETURNDATACOPY" => RETURNDATACOPY,
        "extcodehash"|"EXTCODEHASH" => EXTCODEHASH,
        // 40s: Block Information
        "blockhash"|"BLOCKHASH" => BLOCKHASH,
        "coinbase"|"COINBASE" => COINBASE,
        "timestamp"|"TIMESTAMP" => TIMESTAMP,
        "number"|"NUMBER" => NUMBER,
        "difficulty"|"DIFFICULTY" => DIFFICULTY,
        "gaslimit"|"GASLIMIT" => GASLIMIT,
        "chainid"|"CHAINID" => CHAINID,
        "selfbalance"|"SELFBALANCE" => SELFBALANCE,
        // 50s: Stack, Memory, Storage and Flow Operations
        "pop"|"POP" => POP,
        "mload"|"MLOAD" => MLOAD,
        "mstore"|"MSTORE" => MSTORE,
        "mstore8"|"MSTORE8" => MSTORE8,
        "sload"|"SLOAD" => SLOAD,
        "sstore"|"SSTORE" => SSTORE,
        "jump"|"JUMP" => JUMP,
        "jumpi"|"JUMPI" => JUMPI,
        "pc"|"PC" => PC,
        "msize"|"MSIZE" => MSIZE,
        "gas"|"GAS" => GAS,
        "jumpdest"|"JUMPDEST" => JUMPDEST,
        // 60s & 70s: Push Operations
        "push"|"PUSH" => {
            // Should be impossible to get here!
            unreachable!();
        }
        // 80s: Duplication Operations
        "dup1"|"DUP1" => DUP(1),
        "dup2"|"DUP2" => DUP(2),
        "dup3"|"DUP3" => DUP(3),
        "dup4"|"DUP4" => DUP(4),
        "dup5"|"DUP5" => DUP(5),
        "dup6"|"DUP6" => DUP(6),
        "dup7"|"DUP7" => DUP(7),
        "dup8"|"DUP8" => DUP(8),
        "dup9"|"DUP9" => DUP(9),
        "dup10"|"DUP10" => DUP(10),
        "dup11"|"DUP11" => DUP(11),
        "dup12"|"DUP12" => DUP(12),
        "dup13"|"DUP13" => DUP(13),
        "dup14"|"DUP14" => DUP(14),
        "dup15"|"DUP15" => DUP(15),
        "dup16"|"DUP16" => DUP(16),
        // 90s: Swap Operations
        "swap1"|"SWAP1" => SWAP(1),
        "swap2"|"SWAP2" => SWAP(2),
        "swap3"|"SWAP3" => SWAP(3),
        "swap4"|"SWAP4" => SWAP(4),
        "swap5"|"SWAP5" => SWAP(5),
        "swap6"|"SWAP6" => SWAP(6),
        "swap7"|"SWAP7" => SWAP(7),
        "swap8"|"SWAP8" => SWAP(8),
        "swap9"|"SWAP9" => SWAP(9),
        "swap10"|"SWAP10" => SWAP(10),
        "swap11"|"SWAP11" => SWAP(11),
        "swap12"|"SWAP12" => SWAP(12),
        "swap13"|"SWAP13" => SWAP(13),
        "swap14"|"SWAP14" => SWAP(14),
        "swap15"|"SWAP15" => SWAP(15),
        "swap16"|"SWAP16" => SWAP(16),
        // a0s: Log Operations
        "log0"|"LOG0" => LOG(0),
        "log1"|"LOG1" => LOG(1),
        "log2"|"LOG2" => LOG(2),
        "log3"|"LOG3" => LOG(3),
        "log4"|"LOG4" => LOG(4),
        // f0s: System Operations
        "create"|"CREATE" => CREATE,
        "call"|"CALL" => CALL,
        "callcode"|"CALLCODE" => CALLCODE,
        "return"|"RETURN" => RETURN,
        "delegatecall"|"DELEGATECALL" => DELEGATECALL,
        "create2"|"CREATE2" => CREATE2,
        "staticcall"|"STATICCALL" => STATICCALL,
        "revert"|"REVERT" => REVERT,
        "invalid"|"INVALID" => INVALID,
        "selfdestruct"|"SELFDESTRUCT" => SELFDESTRUCT,
        //
        _ => {
            println!("{insn}");
            return Err(AssemblyError::InvalidInstruction);
        }
    };
    //
    Ok(insn)
}