File Coverage

lib/SPVM/Builder/src/spvm_opcode_list.c
Criterion Covered Total %
statement 22 24 91.6
branch 3 4 75.0
condition n/a
subroutine n/a
pod n/a
total 25 28 89.2


line stmt bran cond sub pod time code
1             // Copyright (c) 2023 Yuki Kimoto
2             // MIT License
3              
4             #include
5             #include
6             #include
7              
8             #include "spvm_opcode_list.h"
9             #include "spvm_allocator.h"
10             #include "spvm_compiler.h"
11             #include "spvm_opcode.h"
12              
13 310960           SPVM_OPCODE_LIST* SPVM_OPCODE_LIST_new(SPVM_COMPILER* compiler) {
14             (void)compiler;
15            
16 310960           SPVM_OPCODE_LIST* opcodes = SPVM_ALLOCATOR_alloc_memory_block_permanent(compiler->current_each_compile_allocator, sizeof(SPVM_OPCODE_LIST));
17 310960           opcodes->capacity = 64;
18 310960           opcodes->length = 0;
19            
20 310960           int64_t values_size = (int64_t)opcodes->capacity * (int64_t)sizeof(SPVM_OPCODE);
21 310960           SPVM_OPCODE* values = SPVM_ALLOCATOR_alloc_memory_block_permanent(compiler->current_each_compile_allocator, values_size);
22 310960           opcodes->values = values;
23            
24 310960           return opcodes;
25             }
26              
27 9483674           void SPVM_OPCODE_LIST_push_opcode(SPVM_COMPILER* compiler, SPVM_OPCODE_LIST* opcodes, SPVM_OPCODE* opcode) {
28             (void)compiler;
29              
30 9483674           int32_t length = opcodes->length;
31 9483674           int32_t capacity = opcodes->capacity;
32            
33 9483674 100         if (length >= capacity) {
34 62857           int32_t new_capacity = capacity * 2;
35            
36 62857           int64_t new_values_size = (int64_t)new_capacity * (int64_t)sizeof(SPVM_OPCODE);
37 62857           SPVM_OPCODE* new_values = SPVM_ALLOCATOR_alloc_memory_block_permanent(compiler->current_each_compile_allocator, new_values_size);
38 62857           memcpy(new_values, opcodes->values, capacity * sizeof(SPVM_OPCODE));
39 62857           opcodes->values = new_values;
40            
41 62857           opcodes->capacity = new_capacity;
42             }
43            
44 9483674 50         assert(opcode);
45 9483674           memcpy(&opcodes->values[length], opcode, sizeof(SPVM_OPCODE));
46              
47 9483674           opcodes->length++;
48 9483674           }
49              
50 0           void SPVM_OPCODE_LIST_free(SPVM_COMPILER* compiler, SPVM_OPCODE_LIST* opcodes) {
51             (void)compiler;
52 0           }