libxas/
eaf.rs

1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * libxas: Extendable Assembler Library
4 * Copyright (C) 2022 Amy Parker <apark0006@student.cerritos.edu>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA or visit
19 * the GNU Project at <https://gnu.org/licenses>. The GNU General Public
20 * License version 2 is available at, for your convenience,
21 * <https://gnu.org/licenses/old-licenses/gpl-2.0.html>.
22 */
23
24use std::str::FromStr;
25
26// TODO: proper documentation of everything
27// NOTE: EAF = Easy Assembler Frontend
28// (or Extendible Assembler Frontend)
29// (or Extendible Assembler interFace)
30
31// TODO: function with automatic platform detection
32
33// TODO: provide more options for inputs (files maybe? put them behind features??)
34// TODO: reexport in top-level lib.rs for ease-of-use (look into how to do this properly)
35
36// TODO: this is a library, so good error handling is a must
37// need to get rid of ?s and .unwrap()s, and return a Result
38
39// Parser is type-neutral, but everything from then on isn't...
40pub fn assemble_full_source(src: &String, pl: &crate::platform::Platform) -> Vec<u8> {
41    let mut p: crate::parser::Parser = crate::parser::Parser::from_str(src).unwrap();
42    p.parse_all();
43    log::trace!("eaf: parser stage completed, assembling file");
44    // if only rust could return types from matches...
45    match pl.arch {
46        // code dup bt c8r, c8 TODO
47        #[cfg(feature = "chip8-raw")]
48        crate::platform::PlatformArch::ChipEightRaw => assemble_full_source_gen::<
49            crate::bbu::chip8_raw::Chip8Symbol,
50            crate::bbu::chip8_raw::Chip8PtrSize,
51        >(p.pop_vdq(), pl),
52        #[cfg(feature = "chip8")]
53        crate::platform::PlatformArch::ChipEight => assemble_full_source_gen::<
54            crate::bbu::chip8_raw::Chip8Symbol,
55            crate::bbu::chip8_raw::Chip8PtrSize,
56        >(p.pop_vdq(), pl), //_ => panic!("unknown arch")
57    }
58}
59
60// naturally handles lexer-onwards
61pub fn assemble_full_source_gen<T: crate::bbu::SymConv, U: crate::bbu::PtrSize>(
62    s: std::collections::VecDeque<crate::parser::ParsedOperation>,
63    p: &crate::platform::Platform,
64) -> Vec<u8> {
65    let mut l: crate::lexer::Lexer<T> = crate::lexer::Lexer::from_vdq(s, p.clone());
66    l.lex_full_queue().expect("temporary error handling");
67    let mut r: Vec<u8> = Vec::new();
68    crate::bbu::outs::run_output::<T, U>(l.pop_vdq(), &mut r, p);
69    r
70}
71
72// Copied TODOs from main:
73// - TODO: have callee clone, or better yet, avoid cloning at all... see below
74// - TODO: migrate as much as possible to &String or &str depending on context (not in structs)
75// - TODO: pass as many things by reference as possible... question is, is it better to take ownership
76//         (guaranteed only shows once) or limit function parameter transfer (faster, risks 2x:1 leak)
77//         if going with more pass-by-reference, more memory usage auditing needs to be done