9 calculation instructions add sub and or xor - #36
Merged
Conversation
…into the instruction execution table
…calculation-instructions-add-sub-and-or-xor
Contributor
There was a problem hiding this comment.
Pull request overview
Adds support for Corewar “calculation” instructions by implementing the missing opcode handlers and wiring them into the VM’s opcode dispatch.
Changes:
- Added implementations for
add,sub,and,or,xorinstruction handlers. - Updated the process execution dispatcher to call the new handlers and made instruction fetch wrap safely in memory.
- Registered the new source file in the build and exposed prototypes in the public header.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
src/instructions/instructions_calc.c |
Implements op_add, op_sub, op_and, op_or, op_xor plus argument decoding helpers. |
src/core/execute_processes.c |
Adds opcode→function dispatch entries for the new instructions; wraps opcode fetch by MEM_SIZE. |
Makefile |
Builds the newly added instruction source file. |
include/corewar.h |
Declares new instruction handlers for linking/dispatch. |
Comments suppressed due to low confidence (3)
src/instructions/instructions_calc.c:85
- Same issue as
op_add: returning early on invalid registers leavesproc->pcunchanged, which can lock the process on the same invalidsubinstruction indefinitely. Advance the PC by the full instruction size (5 bytes) even when arguments are invalid.
if (r1 < 0 || r1 >= REG_NUMBER || r2 < 0 || r2 >= REG_NUMBER)
return;
if (r3 < 0 || r3 >= REG_NUMBER)
return;
src/instructions/instructions_calc.c:124
- Same as
op_and: validatetypes[]againstop_tabforor(especially ensuring the 3rd arg isT_REG) and ensure PC advancement matches the actual encoded argument sizes when the coding byte is invalid.
decode_coding_byte(vm->arena[(proc->pc + 1) % MEM_SIZE], types);
v1 = get_val(vm, proc, types[0], &pos);
v2 = get_val(vm, proc, types[1], &pos);
r3 = vm->arena[pos % MEM_SIZE] - 1;
if (r3 >= 0 && r3 < REG_NUMBER) {
src/instructions/instructions_calc.c:144
- Same as
op_and: validatetypes[]againstop_tabforxor(especially ensuring the 3rd arg isT_REG) and ensure PC advancement matches the actual encoded argument sizes when the coding byte is invalid.
decode_coding_byte(vm->arena[(proc->pc + 1) % MEM_SIZE], types);
v1 = get_val(vm, proc, types[0], &pos);
v2 = get_val(vm, proc, types[1], &pos);
r3 = vm->arena[pos % MEM_SIZE] - 1;
if (r3 >= 0 && r3 < REG_NUMBER) {
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+66
to
+69
| if (r1 < 0 || r1 >= REG_NUMBER || r2 < 0 || r2 >= REG_NUMBER) | ||
| return; | ||
| if (r3 < 0 || r3 >= REG_NUMBER) | ||
| return; |
Comment on lines
+33
to
+38
| int addr = 0; | ||
| int target = 0; | ||
| int val = 0; | ||
|
|
||
| for (int i = 0; i < 2; i++) | ||
| addr = (addr << 8) | vm->arena[(*pos + i) % MEM_SIZE]; |
Comment on lines
+100
to
+104
| decode_coding_byte(vm->arena[(proc->pc + 1) % MEM_SIZE], types); | ||
| v1 = get_val(vm, proc, types[0], &pos); | ||
| v2 = get_val(vm, proc, types[1], &pos); | ||
| r3 = vm->arena[pos % MEM_SIZE] - 1; | ||
| if (r3 >= 0 && r3 < REG_NUMBER) { |
Comment on lines
+59
to
+63
| void op_add(global_t *global, vm_t *vm, process_t *proc) | ||
| { | ||
| int r1 = vm->arena[(proc->pc + 2) % MEM_SIZE] - 1; | ||
| int r2 = vm->arena[(proc->pc + 3) % MEM_SIZE] - 1; | ||
| int r3 = vm->arena[(proc->pc + 4) % MEM_SIZE] - 1; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.