yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
code_gen_tool.h
Go to the documentation of this file.
1
12#ifndef YAZE_SRC_CLI_SERVICE_AGENT_TOOLS_CODE_GEN_TOOL_H_
13#define YAZE_SRC_CLI_SERVICE_AGENT_TOOLS_CODE_GEN_TOOL_H_
14
15#include <cstdint>
16#include <map>
17#include <string>
18#include <vector>
19
20#include "absl/status/status.h"
21#include "absl/status/statusor.h"
23
24namespace yaze {
25namespace cli {
26namespace agent {
27namespace tools {
28
33 std::string name;
34 std::string code_template; // Uses {{PLACEHOLDER}} syntax
35 std::vector<std::string> required_params;
36 std::string description;
37};
38
43 uint32_t start;
44 uint32_t end;
45 std::string description;
46 int free_percent; // Percentage of region that is 0x00/0xFF
47
48 size_t Size() const { return end - start + 1; }
49};
50
55 enum class Severity { kInfo, kWarning, kError };
56
58 std::string message;
59 uint32_t address = 0;
60
61 std::string SeverityString() const {
62 switch (severity) {
63 case Severity::kInfo:
64 return "info";
66 return "warning";
68 return "error";
69 }
70 return "unknown";
71 }
72};
73
78 bool success;
79 std::string generated_code;
80 std::vector<CodeGenerationDiagnostic> diagnostics;
81 std::map<std::string, uint32_t> symbols; // Label -> address
82
83 void AddInfo(const std::string& message, uint32_t address = 0) {
84 diagnostics.push_back(
86 }
87
88 void AddWarning(const std::string& message, uint32_t address = 0) {
89 diagnostics.push_back(
91 }
92
93 void AddError(const std::string& message, uint32_t address = 0) {
94 success = false;
95 diagnostics.push_back(
97 }
98};
99
110 protected:
114 absl::Status ValidateHookAddress(Rom* rom, uint32_t address) const;
115
119 std::vector<FreeSpaceRegion> DetectFreeSpace(Rom* rom,
120 size_t min_size = 0x100) const;
121
127 std::string SubstitutePlaceholders(
128 const std::string& template_code,
129 const std::map<std::string, std::string>& params) const;
130
134 absl::StatusOr<AsmTemplate> GetTemplate(const std::string& name) const;
135
139 const std::vector<AsmTemplate>& GetAllTemplates() const;
140
144 std::string FormatResultAsJson(const CodeGenerationResult& result) const;
145
149 std::string FormatResultAsText(const CodeGenerationResult& result) const;
150
154 bool IsKnownHookLocation(uint32_t address) const;
155
159 std::string GetHookLocationDescription(uint32_t address) const;
160
161 private:
162 static std::vector<AsmTemplate> InitializeTemplates();
163 static const std::vector<AsmTemplate> kTemplates;
164
165 // Known safe hook locations (routine_name -> address)
166 static const std::map<std::string, uint32_t> kKnownHooks;
167};
168
178 public:
179 std::string GetName() const override { return "codegen-asm-hook"; }
180
181 std::string GetDescription() const {
182 return "Generate ASM hook at ROM address";
183 }
184
185 std::string GetUsage() const override {
186 return "codegen-asm-hook --address=0x008040 --label=MyHook [--nop-fill=N] "
187 "[--format=<json|text>]";
188 }
189
190 protected:
191 absl::Status ValidateArgs(const resources::ArgumentParser& parser) override {
192 return parser.RequireArgs({"address", "label"});
193 }
194
195 absl::Status Execute(Rom* rom, const resources::ArgumentParser& parser,
196 resources::OutputFormatter& formatter) override;
197
198 bool RequiresRom() const override { return true; }
199};
200
210 public:
211 std::string GetName() const override { return "codegen-freespace-patch"; }
212
213 std::string GetDescription() const {
214 return "Generate patch using detected free regions";
215 }
216
217 std::string GetUsage() const override {
218 return "codegen-freespace-patch --label=MyCode [--size=0x100] "
219 "[--prefer-bank=N] [--format=<json|text>]";
220 }
221
222 protected:
223 absl::Status ValidateArgs(const resources::ArgumentParser& parser) override {
224 return parser.RequireArgs({"label"});
225 }
226
227 absl::Status Execute(Rom* rom, const resources::ArgumentParser& parser,
228 resources::OutputFormatter& formatter) override;
229
230 bool RequiresRom() const override { return true; }
231};
232
242 public:
243 std::string GetName() const override { return "codegen-sprite-template"; }
244
245 std::string GetDescription() const {
246 return "Generate sprite ASM from template";
247 }
248
249 std::string GetUsage() const override {
250 return "codegen-sprite-template --name=MySprite [--init-code=\"...\"] "
251 "[--main-code=\"...\"] [--format=<json|text>]";
252 }
253
254 protected:
255 absl::Status ValidateArgs(const resources::ArgumentParser& parser) override {
256 return parser.RequireArgs({"name"});
257 }
258
259 absl::Status Execute(Rom* rom, const resources::ArgumentParser& parser,
260 resources::OutputFormatter& formatter) override;
261
262 bool RequiresRom() const override { return false; }
263};
264
274 public:
275 std::string GetName() const override { return "codegen-event-handler"; }
276
277 std::string GetDescription() const { return "Generate event handler code"; }
278
279 std::string GetUsage() const override {
280 return "codegen-event-handler --type=<nmi|irq|reset> --label=MyHandler "
281 "[--custom-code=\"...\"] [--format=<json|text>]";
282 }
283
284 protected:
285 absl::Status ValidateArgs(const resources::ArgumentParser& parser) override {
286 return parser.RequireArgs({"type", "label"});
287 }
288
289 absl::Status Execute(Rom* rom, const resources::ArgumentParser& parser,
290 resources::OutputFormatter& formatter) override;
291
292 bool RequiresRom() const override { return false; }
293};
294
295} // namespace tools
296} // namespace agent
297} // namespace cli
298} // namespace yaze
299
300#endif // YAZE_SRC_CLI_SERVICE_AGENT_TOOLS_CODE_GEN_TOOL_H_
The Rom class is used to load, save, and modify Rom data. This is a generic SNES ROM container and do...
Definition rom.h:52
Generate ASM hook at a specific ROM address.
absl::Status Execute(Rom *rom, const resources::ArgumentParser &parser, resources::OutputFormatter &formatter) override
Execute the command business logic.
absl::Status ValidateArgs(const resources::ArgumentParser &parser) override
Validate command arguments.
std::string GetUsage() const override
Get the command usage string.
std::string GetName() const override
Get the command name.
bool RequiresRom() const override
Check if the command requires a loaded ROM.
bool RequiresRom() const override
Check if the command requires a loaded ROM.
absl::Status ValidateArgs(const resources::ArgumentParser &parser) override
Validate command arguments.
std::string GetUsage() const override
Get the command usage string.
std::string GetName() const override
Get the command name.
absl::Status Execute(Rom *rom, const resources::ArgumentParser &parser, resources::OutputFormatter &formatter) override
Execute the command business logic.
Generate patch using detected freespace regions.
std::string GetUsage() const override
Get the command usage string.
bool RequiresRom() const override
Check if the command requires a loaded ROM.
absl::Status Execute(Rom *rom, const resources::ArgumentParser &parser, resources::OutputFormatter &formatter) override
Execute the command business logic.
std::string GetName() const override
Get the command name.
absl::Status ValidateArgs(const resources::ArgumentParser &parser) override
Validate command arguments.
absl::Status Execute(Rom *rom, const resources::ArgumentParser &parser, resources::OutputFormatter &formatter) override
Execute the command business logic.
std::string GetName() const override
Get the command name.
bool RequiresRom() const override
Check if the command requires a loaded ROM.
std::string GetUsage() const override
Get the command usage string.
absl::Status ValidateArgs(const resources::ArgumentParser &parser) override
Validate command arguments.
Base class for code generation tools.
static const std::map< std::string, uint32_t > kKnownHooks
const std::vector< AsmTemplate > & GetAllTemplates() const
Get all available built-in templates.
std::string SubstitutePlaceholders(const std::string &template_code, const std::map< std::string, std::string > &params) const
Substitute placeholders in template with parameter values.
static const std::vector< AsmTemplate > kTemplates
std::vector< FreeSpaceRegion > DetectFreeSpace(Rom *rom, size_t min_size=0x100) const
Detect available free space regions in ROM.
static std::vector< AsmTemplate > InitializeTemplates()
std::string FormatResultAsText(const CodeGenerationResult &result) const
Format code generation result as text.
absl::StatusOr< AsmTemplate > GetTemplate(const std::string &name) const
Get a built-in ASM template by name.
std::string GetHookLocationDescription(uint32_t address) const
Get description of a known hook location.
std::string FormatResultAsJson(const CodeGenerationResult &result) const
Format code generation result as JSON.
bool IsKnownHookLocation(uint32_t address) const
Check if an address is within a known safe hook location.
absl::Status ValidateHookAddress(Rom *rom, uint32_t address) const
Validate that an address is safe for hooking.
Utility for parsing common CLI argument patterns.
absl::Status RequireArgs(const std::vector< std::string > &required) const
Validate that required arguments are present.
Base class for CLI command handlers.
Utility for consistent output formatting across commands.
ASM code template with placeholder substitution.
std::vector< std::string > required_params
Diagnostic message from code generation.
Result of code generation operation.
std::map< std::string, uint32_t > symbols
std::vector< CodeGenerationDiagnostic > diagnostics
void AddError(const std::string &message, uint32_t address=0)
void AddInfo(const std::string &message, uint32_t address=0)
void AddWarning(const std::string &message, uint32_t address=0)