Skip to content

Commit

Permalink
Bytecode representation
Browse files Browse the repository at this point in the history
  • Loading branch information
dils2k committed Jan 1, 2023
1 parent 225252b commit 19b9162
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 6 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ project(clox C)

set(CMAKE_C_STANDARD 11)

add_executable(clox main.c common.h chunk.h chunk.c memory.h memory.c)
add_executable(clox main.c common.h chunk.h chunk.c memory.h debug.h debug.c memory.c value.h value.c)
20 changes: 18 additions & 2 deletions chunk.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,31 @@
void initChunk(Chunk* chunk) {
chunk->count = 0;
chunk->capacity = 0;
chunk->lines = NULL;
chunk->code = NULL;
initValueArray(&chunk->constants);
}

void writeChunk(Chunk* chunk, uint8_t byte) {
void writeChunk(Chunk* chunk, uint8_t byte, int line) {
if (chunk->capacity < chunk->count + 1) {
int oldCapacity = chunk->capacity;
chunk->capacity = GROW_CAPACITY(oldCapacity);
chunk->code = GROW_ARRAY(uint8_t, chunk->code, oldCapacity, chunk->capacity);
chunk->lines= GROW_ARRAY(int, chunk->lines, oldCapacity, chunk->capacity);
}
chunk->code[chunk->count] = byte;
chunk->lines[chunk->count] = line;
chunk->count++;
}
}

void freeChunk(Chunk* chunk) {
FREE_ARRAY(uint8_t, chunk->code, chunk->capacity);
FREE_ARRAY(int, chunk->lines, chunk->capacity);
freeValueArray(&chunk->constants);
initChunk(chunk);
}

int addConstant(Chunk* chunk, Value value) {
writeValueArray(&chunk->constants, value);
return chunk->constants.count - 1;
}
10 changes: 8 additions & 2 deletions chunk.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@
#define clox_chunk_h

#include "common.h"
#include "value.h"

typedef enum {
OP_RETURN,
OP_CONSTANT
} OpCode;

typedef struct {
int count;
int capacity;
uint8_t* code;
int* lines;
ValueArray constants;
} Chunk;

void initChunk(Chunk* chunk);
void writeChunk(Chunk* chunk, uint8_t byte);
void writeChunk(Chunk* chunk, uint8_t byte, int line);
void freeChunk(Chunk* chunk);
int addConstant(Chunk* chunk, Value value);

#endif
#endif
41 changes: 41 additions & 0 deletions debug.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <stdio.h>

#include "debug.h"

void disassembleChunk(Chunk* chunk, const char* name) {
printf("== %s ==\n", name);

for (int offset = 0; offset < chunk->count;) {
offset = disassembleInstruction(chunk, offset);
}
}

static int simpleInstruction(const char* name, int offset) {
printf("%s\n", name);
return offset + 1;
}

static int constantInstruction(const char* name, Chunk* chunk, int offset) {
uint8_t constant = chunk->code[offset + 1];
printf("%-16s %4d '", name, constant);
printValue(chunk->constants.values[constant]);
printf("'\n");
return offset + 2;
}

int disassembleInstruction(Chunk* chunk, int offset) {
printf("%04d ", offset);
printf("%4d ", chunk->lines[offset]);

uint8_t instruction = chunk->code[offset];
switch (instruction) {
case OP_RETURN:
return simpleInstruction("OP_RETURN", offset);
case OP_CONSTANT:
return constantInstruction("OP_CONSTANT", chunk, offset);
default:
printf("Unknown opcode %d\n", instruction);
return offset + 1;
}
}

9 changes: 9 additions & 0 deletions debug.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef clox_debug_h
#define clox_debug_h

#include "chunk.h"

void disassembleChunk(Chunk* chunk, const char* name);
int disassembleInstruction(Chunk* chunk, int offset);

#endif
14 changes: 14 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
#include "common.h"
#include "chunk.h"
#include "debug.h"

int main(int argc, const char* argv[]) {
Chunk chunk;
initChunk(&chunk);

int constant = addConstant(&chunk, 1.2);
writeChunk(&chunk, OP_CONSTANT, 123);
writeChunk(&chunk, OP_CONSTANT, 123);
writeChunk(&chunk, OP_CONSTANT, 123);
writeChunk(&chunk, OP_CONSTANT, 123);
writeChunk(&chunk, constant, 123);

disassembleChunk(&chunk, "test chunk");
freeChunk(&chunk);
return 0;
}
3 changes: 2 additions & 1 deletion memory.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#include <stdlib.h>

#include "memory.h"
#include "common.h"

void* reallocate(void* pointer, size_t oldSize, size_t newSize) {
if (newSize == 0) {
free(pointer);
return NULL;
}
void* result = realloc(pointer, newSize);
if (result == NULL) exit(1);
return result;
}

5 changes: 5 additions & 0 deletions memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@
#define GROW_ARRAY(type, pointer, oldCount, newCount) \
(type*) reallocate(pointer, sizeof(type) * (oldCount), sizeof(type) * (newCount))

#define FREE_ARRAY(type, pointer, oldCount) \
reallocate(pointer, sizeof(type) * (oldCount), 0)

void* reallocate(void* pointer, size_t oldSize, size_t newSize);

#endif
29 changes: 29 additions & 0 deletions value.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <stdio.h>

#include "memory.h"
#include "value.h"

void initValueArray(ValueArray* array) {
array->values = NULL;
array->capacity = 0;
array->count = 0;
}

void writeValueArray(ValueArray* array, Value value) {
if (array->capacity < array->count + 1) {
int oldCapacity = array->capacity;
array->capacity = GROW_CAPACITY(oldCapacity);
array->values = GROW_ARRAY(Value, array->values, oldCapacity, array->capacity);
}
array->values[array->count] = value;
array->count++;
}

void freeValueArray(ValueArray* array) {
FREE_ARRAY(Value, array->values, array->capacity);
initValueArray(array);
}

void printValue(Value value) {
printf("%g", value);
}
17 changes: 17 additions & 0 deletions value.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef clox_value_h
#define clox_value_h

typedef double Value;

typedef struct {
int capacity;
int count;
Value* values;
} ValueArray;

void initValueArray(ValueArray* array);
void writeValueArray(ValueArray* array, Value value);
void freeValueArray(ValueArray* array);
void printValue(Value value);

#endif

0 comments on commit 19b9162

Please sign in to comment.