Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scriptcomp: Fold equivalent instructions #70

Open
mtijanic opened this issue Jul 13, 2023 · 0 comments
Open

scriptcomp: Fold equivalent instructions #70

mtijanic opened this issue Jul 13, 2023 · 0 comments
Assignees

Comments

@mtijanic
Copy link
Collaborator

Sample script

void main() {
    if (1) {
        string s = "AAA";
    }
}

gets compiled down to

     0  JSR, 8                                  
     6  RET                                     
     8  CONSTANT, TYPE_INTEGER, 1               
    14  JZ, 41                                  
    20  RUNSTACK_ADD, TYPE_STRING               
    22  CONSTANT, TYPE_STRING, "AAA"            
    29  ASSIGNMENT, TYPE_VOID, -8, 4            
    37  MODIFY_STACK_POINTER, -4                
    43  MODIFY_STACK_POINTER, -4                
    49  JMP, 6                                  
    55  RET         

the two MODIFY_STACK_POINTER instructions could be a single one. There are other such instances.

I think for this to work, we probably need another pass over the generated NCS before writing it out, and then fixing up the jumps.
Alternatively, we can just keep track of the last instruction, and if it is the same as current, we modify it instead.

@mtijanic mtijanic self-assigned this Jul 15, 2023
mtijanic added a commit that referenced this issue Jul 15, 2023
When the codegen produces multiple MODIFY_STACK_POINTER instructions in
a row, we instead just keep the one and modify the value. Plus some
scaffolding to make this and future changes easier

Partial fix for #70

## Testing

Test script:
```nss
void main() {
    if (1) {
        string s = "AAA";
    }
}
```
Before this change, it compiles as
```
     0  JSR, 8                                  
     6  RET                                     
     8  CONSTANT, TYPE_INTEGER, 1               
    14  JZ, 41                                  
    20  RUNSTACK_ADD, TYPE_STRING               
    22  CONSTANT, TYPE_STRING, "AAA"            
    29  ASSIGNMENT, TYPE_VOID, -8, 4            
    37  MODIFY_STACK_POINTER, -4                
    43  MODIFY_STACK_POINTER, -4                
    49  JMP, 6                                  
    55  RET 
```
and after:
```
     0  JSR, 8                                  
     6  RET                                     
     8  CONSTANT, TYPE_INTEGER, 1               
    14  JZ, 35                                  
    20  RUNSTACK_ADD, TYPE_STRING               
    22  CONSTANT, TYPE_STRING, "AAA"            
    29  ASSIGNMENT, TYPE_VOID, -8, 4            
    37  MODIFY_STACK_POINTER, -8                
    43  JMP, 6                                  
    49  RET  
```

No changelog entry for now, as it's minor on its own, but other such
things will come.

## Licence

- [x] I am licencing my change under the project's MIT licence,
including all changes to GPL-3.0 licenced parts of the codebase.
mtijanic added a commit that referenced this issue Jul 16, 2023
The nwscript construct `int n = 3;` gets compiled into the following:
```
    RUNSTACK_ADD, TYPE_INTEGER
    CONSTANT, TYPE_INTEGER, 3
    ASSIGNMENT, TYPE_VOID, -8, 4
    MODIFY_STACK_POINTER, -4
```
This ends up with just the CONSTI 3 on the top of stack, but the
dance is necessary as `n = 3` is also an expression which returns 3.
Then, the last MODSP discards the result of the expression.
Here, we detect the pattern when it is discarded, and replace the
whole set of instructions with just CONSTI 3

Partial fix for #70 

## Testing

Added variables.nss test. Prior to this change, it got compiled into
this mess (without asserts):
```
     0  JSR, 8                                  
     6  RET                                     
     8  RUNSTACK_ADD, TYPE_INTEGER              
    10  CONSTANT, TYPE_INTEGER, 1               
    16  ASSIGNMENT, TYPE_VOID, -8, 4            
    24  MODIFY_STACK_POINTER, -4                
    30  RUNSTACK_ADD, TYPE_INTEGER              
    32  CONSTANT, TYPE_INTEGER, 2               
    38  ASSIGNMENT, TYPE_VOID, -8, 4            
    46  MODIFY_STACK_POINTER, -4                
    52  RUNSTACK_ADD, TYPE_INTEGER              
    54  CONSTANT, TYPE_INTEGER, 3               
    60  ASSIGNMENT, TYPE_VOID, -8, 4            
    68  MODIFY_STACK_POINTER, -4                
    74  RUNSTACK_ADD, TYPE_INTEGER              
    76  CONSTANT, TYPE_INTEGER, 4               
    82  ASSIGNMENT, TYPE_VOID, -8, 4            
    90  MODIFY_STACK_POINTER, -4                
    96  RUNSTACK_ADD, TYPE_STRING               
    98  CONSTANT, TYPE_STRING, "A"              
   103  ASSIGNMENT, TYPE_VOID, -8, 4            
   111  MODIFY_STACK_POINTER, -4                
   117  RUNSTACK_ADD, TYPE_FLOAT                
   119  CONSTANT, TYPE_FLOAT, 1.0               
   125  ASSIGNMENT, TYPE_VOID, -8, 4            
   133  MODIFY_STACK_POINTER, -4                
   139  CONSTANT, TYPE_INTEGER, 1               
   145  JZ, 143                                 
   151  RUNSTACK_ADD, TYPE_INTEGER              
   153  CONSTANT, TYPE_INTEGER, 10              
   159  ASSIGNMENT, TYPE_VOID, -8, 4            
   167  MODIFY_STACK_POINTER, -4                
   173  RUNSTACK_ADD, TYPE_INTEGER              
   175  CONSTANT, TYPE_INTEGER, 20              
   181  ASSIGNMENT, TYPE_VOID, -8, 4            
   189  MODIFY_STACK_POINTER, -4                
   195  RUNSTACK_ADD, TYPE_INTEGER              
   197  CONSTANT, TYPE_INTEGER, 30              
   203  ASSIGNMENT, TYPE_VOID, -8, 4            
   211  MODIFY_STACK_POINTER, -4                
   217  RUNSTACK_ADD, TYPE_INTEGER              
   219  CONSTANT, TYPE_INTEGER, 40              
   225  ASSIGNMENT, TYPE_VOID, -8, 4            
   233  MODIFY_STACK_POINTER, -4                
   239  RUNSTACK_ADD, TYPE_STRING               
   241  CONSTANT, TYPE_STRING, "B"              
   246  ASSIGNMENT, TYPE_VOID, -8, 4            
   254  MODIFY_STACK_POINTER, -4                
   260  RUNSTACK_ADD, TYPE_FLOAT                
   262  CONSTANT, TYPE_FLOAT, 10.0              
   268  ASSIGNMENT, TYPE_VOID, -8, 4            
   276  MODIFY_STACK_POINTER, -28               
   282  JMP, 6                                  
   288  MODIFY_STACK_POINTER, -24               
   294  RET                                     
```
after the change, it becomes:
```
     0  JSR, 8                                  
     6  RET                                     
     8  CONSTANT, TYPE_INTEGER, 1               
    14  CONSTANT, TYPE_INTEGER, 2               
    20  CONSTANT, TYPE_INTEGER, 3               
    26  CONSTANT, TYPE_INTEGER, 4               
    32  CONSTANT, TYPE_STRING, "A"              
    37  CONSTANT, TYPE_FLOAT, 1.0               
    43  CONSTANT, TYPE_INTEGER, 1               
    49  JZ, 53                                  
    55  CONSTANT, TYPE_INTEGER, 10              
    61  CONSTANT, TYPE_INTEGER, 20              
    67  CONSTANT, TYPE_INTEGER, 30              
    73  CONSTANT, TYPE_INTEGER, 40              
    79  CONSTANT, TYPE_STRING, "B"              
    84  CONSTANT, TYPE_FLOAT, 10.0              
    90  MODIFY_STACK_POINTER, -24               
    96  JMP, 6                                  
   102  MODIFY_STACK_POINTER, -24               
   108  RET                            
```

## Changelog

### Performance Improvements
- nwscript compiler will now generate more optimal code for assigning
values to variables

## Licence

- [x] I am licencing my change under the project's MIT licence,
including all changes to GPL-3.0 licenced parts of the codebase.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant