Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #10694 from Pokechu22/dsp-assembler-error-messages…
…-etc

DSPAssembler: Rework errors and warnings, and related cleanup
  • Loading branch information
Tilka committed May 28, 2022
2 parents 57e444c + bd3173e commit 3dbc180
Show file tree
Hide file tree
Showing 11 changed files with 286 additions and 245 deletions.
298 changes: 158 additions & 140 deletions Source/Core/Core/DSP/DSPAssembler.cpp

Large diffs are not rendered by default.

26 changes: 22 additions & 4 deletions Source/Core/Core/DSP/DSPAssembler.h
Expand Up @@ -6,9 +6,13 @@

#include <cstddef>
#include <map>
#include <optional>
#include <string>
#include <string_view>
#include <vector>

#include <fmt/format.h>

#include "Common/CommonTypes.h"

#include "Core/DSP/DSPDisassembler.h"
Expand Down Expand Up @@ -85,6 +89,17 @@ class DSPAssembler
Extension
};

struct LocationContext
{
u32 line_num = 0;
std::string line_text;
std::optional<OpcodeType> opcode_type;
std::optional<size_t> opcode_param_number;
std::optional<std::string> included_file_path;
};

friend struct ::fmt::formatter<DSP::DSPAssembler::LocationContext>;

// Utility functions
s32 ParseValue(const char* str);
u32 ParseExpression(const char* ptr);
Expand All @@ -94,7 +109,11 @@ class DSPAssembler
void InitPass(int pass);
bool AssemblePass(const std::string& text, int pass);

void ShowError(AssemblerError err_code, const char* extra_info = nullptr);
void ShowError(AssemblerError err_code);
template <typename... Args>
void ShowError(AssemblerError err_code, fmt::format_string<Args...> format, Args&&... args);
template <typename... Args>
void ShowWarning(fmt::format_string<Args...> format, Args&&... args);

char* FindBrackets(char* src, char* dst);
const DSPOPCTemplate* FindOpcode(std::string name, size_t par_count, OpcodeType type);
Expand All @@ -104,15 +123,13 @@ class DSPAssembler
std::vector<u16> m_output_buffer;

std::string m_include_dir;
std::string m_cur_line;

u32 m_cur_addr = 0;
int m_total_size = 0;
u8 m_cur_pass = 0;

LabelMap m_labels;

u32 m_code_line = 0;
bool m_failed = false;
std::string m_last_error_str;
AssemblerError m_last_error = AssemblerError::OK;
Expand All @@ -122,7 +139,8 @@ class DSPAssembler

segment_t m_cur_segment = SEGMENT_CODE;
u32 m_segment_addr[SEGMENT_MAX] = {};
int m_current_param = 0;
const AssemblerSettings m_settings;

LocationContext m_location;
};
} // namespace DSP
8 changes: 1 addition & 7 deletions Source/Core/Core/DSP/DSPCodeUtil.cpp
Expand Up @@ -35,13 +35,7 @@ bool Assemble(const std::string& text, std::vector<u16>& code, bool force)

// TODO: fix the terrible api of the assembler.
DSPAssembler assembler(settings);
if (!assembler.Assemble(text, code))
{
std::cerr << assembler.GetErrorString() << std::endl;
return false;
}

return true;
return assembler.Assemble(text, code);
}

bool Disassemble(const std::vector<u16>& code, bool line_numbers, std::string& text)
Expand Down
16 changes: 12 additions & 4 deletions Source/Core/Core/DSP/DSPCore.h
Expand Up @@ -122,15 +122,23 @@ enum : int
DSP_REG_AXH1 = 0x1b,

// Accumulator (global)
DSP_REG_ACC0 = 0x1c,
DSP_REG_ACC1 = 0x1d,

DSP_REG_ACL0 = 0x1c, // Low accumulator
DSP_REG_ACL1 = 0x1d,
DSP_REG_ACM0 = 0x1e, // Mid accumulator
DSP_REG_ACM1 = 0x1f,
DSP_REG_ACH0 = 0x10, // Sign extended 8 bit register 0
DSP_REG_ACH1 = 0x11 // Sign extended 8 bit register 1
DSP_REG_ACH1 = 0x11, // Sign extended 8 bit register 1
};

enum : int
{
// Magic values used by DSPTables.h
// These do not correspond to real registers like above, but instead combined versions of the
// registers.
DSP_REG_ACC0_FULL = 0x20,
DSP_REG_ACC1_FULL = 0x21,
DSP_REG_AX0_FULL = 0x22,
DSP_REG_AX1_FULL = 0x23,
};

// Hardware registers address
Expand Down
4 changes: 1 addition & 3 deletions Source/Core/Core/DSP/DSPDisassembler.cpp
Expand Up @@ -152,10 +152,8 @@ bool DSPDisassembler::DisassembleOpcode(const u16* binbuf, u16* pc, std::string&

// Find main opcode
const DSPOPCTemplate* opc = FindOpInfoByOpcode(op1);
const DSPOPCTemplate fake_op = {"CW", 0x0000, 0x0000, 1, 1, {{P_VAL, 2, 0, 0, 0xffff}},
false, false, false, false, false};
if (!opc)
opc = &fake_op;
opc = &cw;

bool is_extended = false;
bool is_only_7_bit_ext = false;
Expand Down
130 changes: 65 additions & 65 deletions Source/Core/Core/DSP/DSPTables.cpp

Large diffs are not rendered by default.

31 changes: 16 additions & 15 deletions Source/Core/Core/DSP/DSPTables.h
Expand Up @@ -11,6 +11,7 @@
#include <string_view>

#include "Core/DSP/DSPCommon.h"
#include "Core/DSP/DSPCore.h"

namespace DSP
{
Expand All @@ -31,23 +32,23 @@ enum partype_t
P_ADDR_I = 0x0005,
P_ADDR_D = 0x0006,
P_REG = 0x8000,
P_REG04 = P_REG | 0x0400, // IX
P_REG08 = P_REG | 0x0800,
P_REG18 = P_REG | 0x1800,
P_REGM18 = P_REG | 0x1810, // used in multiply instructions
P_REG19 = P_REG | 0x1900,
P_REGM19 = P_REG | 0x1910, // used in multiply instructions
P_REG1A = P_REG | 0x1a80,
P_REG04 = P_REG | DSP_REG_IX0 << 8,
P_REG08 = P_REG | DSP_REG_WR0 << 8,
P_REG18 = P_REG | DSP_REG_AXL0 << 8,
P_REGM18 = P_REG | DSP_REG_AXL0 << 8 | 0x10, // used in multiply instructions
P_REG19 = P_REG | DSP_REG_AXL1 << 8,
P_REGM19 = P_REG | DSP_REG_AXL1 << 8 | 0x10, // used in multiply instructions
P_REG1A = P_REG | DSP_REG_AXH0 << 8 | 0x80,
// P_ACC = P_REG | 0x1c10, // used for global accum (gcdsptool's value)
P_ACCL = P_REG | 0x1c00, // used for low part of accum
P_REG1C = P_REG | 0x1c10, // gcdsptool calls this P_ACCLM
P_ACCM = P_REG | 0x1e00, // used for mid part of accum
P_ACCL = P_REG | DSP_REG_ACL0 << 8, // used for low part of accum
P_REG1C = P_REG | DSP_REG_ACL0 << 8 | 0x10, // gcdsptool calls this P_ACCLM
P_ACCM = P_REG | DSP_REG_ACM0 << 8, // used for mid part of accum
// The following are not in gcdsptool
P_ACCM_D = P_REG | 0x1e80,
P_ACC = P_REG | 0x2000, // used for full accum.
P_ACCH = P_REG | 0x1000, // used for high part of accum
P_ACC_D = P_REG | 0x2080,
P_AX = P_REG | 0x2200,
P_ACCM_D = P_REG | DSP_REG_ACM0 << 8 | 0x80,
P_ACC = P_REG | DSP_REG_ACC0_FULL << 8, // used for full accum.
P_ACCH = P_REG | DSP_REG_ACH0 << 8, // used for high part of accum
P_ACC_D = P_REG | DSP_REG_ACC0_FULL << 8 | 0x80,
P_AX = P_REG | DSP_REG_AX0_FULL << 8,
P_REGS_MASK = 0x03f80, // gcdsptool's value = 0x01f80
P_REF = P_REG | 0x4000,
P_PRG = P_REF | P_REG,
Expand Down
3 changes: 3 additions & 0 deletions Source/DSPSpy/tests/arith_test.ds
Expand Up @@ -138,3 +138,6 @@ sub $acc1, $acc0
set16

call send_back ; 20

; We're done, DO NOT DELETE THIS LINE
jmp end_of_test
8 changes: 4 additions & 4 deletions Source/DSPSpy/tests/cond_test.ds
Expand Up @@ -213,19 +213,19 @@ test_cond:
ADDARN $AR0, $IX0

LRI $IX0, #0x0100
CW 0x0278 ; IFx8
IFx8
ADDARN $AR0, $IX0

LRI $IX0, #0x0200
CW 0x0279 ; IFx9
IFx9
ADDARN $AR0, $IX0

LRI $IX0, #0x0400
CW 0x027A ; IFxA
IFxA
ADDARN $AR0, $IX0

LRI $IX0, #0x0800
CW 0x027B ; IFxB
IFxB
ADDARN $AR0, $IX0

LRI $IX0, #0x1000
Expand Down
4 changes: 2 additions & 2 deletions Source/DSPSpy/tests/ld_test.ds
Expand Up @@ -244,5 +244,5 @@ lri $AX1.L, #0x13
nx'ldnm : $AX0.L, $AX1.L, @$AR0
call send_back ; 20



; We're done, DO NOT DELETE THIS LINE
jmp end_of_test
3 changes: 2 additions & 1 deletion Source/DSPSpy/tests/neg_test.ds
Expand Up @@ -114,4 +114,5 @@ neg $ACC0
set40
call send_back ; 18


; We're done, DO NOT DELETE THIS LINE
jmp end_of_test

0 comments on commit 3dbc180

Please sign in to comment.