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({CodeGenerationDiagnostic::Severity::kInfo, message, address});
85 }
86
87 void AddWarning(const std::string& message, uint32_t address = 0) {
88 diagnostics.push_back({CodeGenerationDiagnostic::Severity::kWarning, message, address});
89 }
90
91 void AddError(const std::string& message, uint32_t address = 0) {
92 success = false;
93 diagnostics.push_back({CodeGenerationDiagnostic::Severity::kError, message, address});
94 }
95};
96
107 protected:
111 absl::Status ValidateHookAddress(Rom* rom, uint32_t address) const;
112
116 std::vector<FreeSpaceRegion> DetectFreeSpace(Rom* rom, size_t min_size = 0x100) const;
117
123 std::string SubstitutePlaceholders(
124 const std::string& template_code,
125 const std::map<std::string, std::string>& params) const;
126
130 absl::StatusOr<AsmTemplate> GetTemplate(const std::string& name) const;
131
135 const std::vector<AsmTemplate>& GetAllTemplates() const;
136
140 std::string FormatResultAsJson(const CodeGenerationResult& result) const;
141
145 std::string FormatResultAsText(const CodeGenerationResult& result) const;
146
150 bool IsKnownHookLocation(uint32_t address) const;
151
155 std::string GetHookLocationDescription(uint32_t address) const;
156
157 private:
158 static std::vector<AsmTemplate> InitializeTemplates();
159 static const std::vector<AsmTemplate> kTemplates;
160
161 // Known safe hook locations (routine_name -> address)
162 static const std::map<std::string, uint32_t> kKnownHooks;
163};
164
174 public:
175 std::string GetName() const override { return "codegen-asm-hook"; }
176
177 std::string GetDescription() const {
178 return "Generate ASM hook at ROM address";
179 }
180
181 std::string GetUsage() const override {
182 return "codegen-asm-hook --address=0x008040 --label=MyHook [--nop-fill=N] [--format=<json|text>]";
183 }
184
185 protected:
186 absl::Status ValidateArgs(const resources::ArgumentParser& parser) override {
187 return parser.RequireArgs({"address", "label"});
188 }
189
190 absl::Status Execute(Rom* rom, const resources::ArgumentParser& parser,
191 resources::OutputFormatter& formatter) override;
192
193 bool RequiresRom() const override { return true; }
194};
195
205 public:
206 std::string GetName() const override { return "codegen-freespace-patch"; }
207
208 std::string GetDescription() const {
209 return "Generate patch using detected free regions";
210 }
211
212 std::string GetUsage() const override {
213 return "codegen-freespace-patch --label=MyCode --size=0x100 [--prefer-bank=N] [--format=<json|text>]";
214 }
215
216 protected:
217 absl::Status ValidateArgs(const resources::ArgumentParser& parser) override {
218 return parser.RequireArgs({"label"});
219 }
220
221 absl::Status Execute(Rom* rom, const resources::ArgumentParser& parser,
222 resources::OutputFormatter& formatter) override;
223
224 bool RequiresRom() const override { return true; }
225};
226
236 public:
237 std::string GetName() const override { return "codegen-sprite-template"; }
238
239 std::string GetDescription() const {
240 return "Generate sprite ASM from template";
241 }
242
243 std::string GetUsage() const override {
244 return "codegen-sprite-template --name=MySprite [--init-code=\"...\"] [--main-code=\"...\"] [--format=<json|text>]";
245 }
246
247 protected:
248 absl::Status ValidateArgs(const resources::ArgumentParser& parser) override {
249 return parser.RequireArgs({"name"});
250 }
251
252 absl::Status Execute(Rom* rom, const resources::ArgumentParser& parser,
253 resources::OutputFormatter& formatter) override;
254
255 bool RequiresRom() const override { return false; }
256};
257
267 public:
268 std::string GetName() const override { return "codegen-event-handler"; }
269
270 std::string GetDescription() const {
271 return "Generate event handler code";
272 }
273
274 std::string GetUsage() const override {
275 return "codegen-event-handler --type=<nmi|irq|reset> --label=MyHandler [--custom-code=\"...\"] [--format=<json|text>]";
276 }
277
278 protected:
279 absl::Status ValidateArgs(const resources::ArgumentParser& parser) override {
280 return parser.RequireArgs({"type", "label"});
281 }
282
283 absl::Status Execute(Rom* rom, const resources::ArgumentParser& parser,
284 resources::OutputFormatter& formatter) override;
285
286 bool RequiresRom() const override { return false; }
287};
288
289} // namespace tools
290} // namespace agent
291} // namespace cli
292} // namespace yaze
293
294#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:24
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)