Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
t3tra-dev committed Feb 20, 2025
1 parent cb3d86a commit 7852b9e
Show file tree
Hide file tree
Showing 17 changed files with 370 additions and 17,017 deletions.
20 changes: 15 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,26 +1,36 @@
CC = clang
CFLAGS = -Wall -Wextra -O2 -I runtime/builtin $(shell pkg-config --cflags bdw-gc)
LDFLAGS = $(shell pkg-config --libs bdw-gc)
LDFLAGS = $(shell pkg-config --libs bdw-gc) -lgc
RUNTIME_DIR = runtime/builtin
OBJECTS = $(RUNTIME_DIR)/objects/listobject.o $(RUNTIME_DIR)/objects/dictobject.o $(RUNTIME_DIR)/objects/unicodeobject.o $(RUNTIME_DIR)/objects/object.o runtime/runtime.o

# ビルドターゲット
all: runtime.o

# ランタイムライブラリのビルド
runtime.o: $(RUNTIME_DIR)/functions.o $(RUNTIME_DIR)/types.o
runtime.o: $(OBJECTS)
ld -r $^ -o $@

# 個別のオブジェクトファイル
$(RUNTIME_DIR)/functions.o: $(RUNTIME_DIR)/functions.c $(RUNTIME_DIR)/functions.h $(RUNTIME_DIR)/types.h
$(RUNTIME_DIR)/objects/listobject.o: $(RUNTIME_DIR)/objects/listobject.c $(RUNTIME_DIR)/objects/listobject.h
$(CC) $(CFLAGS) -c $< -o $@

$(RUNTIME_DIR)/types.o: $(RUNTIME_DIR)/types.c $(RUNTIME_DIR)/types.h
$(RUNTIME_DIR)/objects/dictobject.o: $(RUNTIME_DIR)/objects/dictobject.c $(RUNTIME_DIR)/objects/dictobject.h
$(CC) $(CFLAGS) -c $< -o $@

$(RUNTIME_DIR)/objects/unicodeobject.o: $(RUNTIME_DIR)/objects/unicodeobject.c $(RUNTIME_DIR)/objects/unicodeobject.h
$(CC) $(CFLAGS) -c $< -o $@

$(RUNTIME_DIR)/objects/object.o: $(RUNTIME_DIR)/objects/object.c $(RUNTIME_DIR)/objects/object.h
$(CC) $(CFLAGS) -c $< -o $@

runtime/runtime.o: runtime/runtime.c runtime/builtin/objects/object.h runtime/builtin/objects/unicodeobject.h runtime/builtin/objects/listobject.h
$(CC) $(CFLAGS) -c $< -o $@

# クリーンアップ
clean:
find . -name '__pycache__' -type d -exec rm -r {} +
rm -f runtime.o
rm -f $(RUNTIME_DIR)/*.o
rm -f $(RUNTIME_DIR)/objects/*.o

.PHONY: all clean
16,727 changes: 0 additions & 16,727 deletions runtime/_bltinmodule.ll

This file was deleted.

186 changes: 0 additions & 186 deletions runtime/builtin/functions.c

This file was deleted.

32 changes: 0 additions & 32 deletions runtime/builtin/functions.h

This file was deleted.

79 changes: 79 additions & 0 deletions runtime/builtin/objects/dictobject.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include <gc.h>
#include "object.h"
#include <string.h>

typedef struct {
PyObject base;
void** keys;
void** values;
size_t size;
size_t capacity;
} PyDictObject;

unsigned int PyDict_Hash(PyDictObject* dict) {
unsigned int hash = 5381;
for (size_t i = 0; i < dict->capacity; i++) {
if (dict->keys[i] != NULL) {
hash = ((hash << 5) + hash) + (unsigned int)(uintptr_t)dict->keys[i];
hash = ((hash << 5) + hash) + (unsigned int)(uintptr_t)dict->values[i];
}
}
return hash;
}

PyDictObject* PyDict_New(size_t initial_capacity) {
PyDictObject* dict = (PyDictObject*)GC_malloc(sizeof(PyDictObject));
PyObject_Init((PyObject*)dict, NULL); // Initialize with no vtable for now
dict->size = 0;
dict->capacity = initial_capacity > 0 ? initial_capacity : 8;
dict->keys = (void**)GC_malloc(sizeof(void*) * dict->capacity);
dict->values = (void**)GC_malloc(sizeof(void*) * dict->capacity);
memset(dict->keys, 0, sizeof(void*) * dict->capacity);
memset(dict->values, 0, sizeof(void*) * dict->capacity);
return dict;
}

void PyDict_SetItem(PyDictObject* dict, void* key, void* value) {
if (dict->size * 2 >= dict->capacity) {
size_t new_capacity = dict->capacity * 2;
void** new_keys = (void**)GC_malloc(sizeof(void*) * new_capacity);
void** new_values = (void**)GC_malloc(sizeof(void*) * new_capacity);
memset(new_keys, 0, sizeof(void*) * new_capacity);
memset(new_values, 0, sizeof(void*) * new_capacity);
for (size_t i = 0; i < dict->capacity; i++) {
if (dict->keys[i] != NULL) {
size_t j = ((size_t)dict->keys[i]) % new_capacity;
while (new_keys[j] != NULL) {
j = (j + 1) % new_capacity;
}
new_keys[j] = dict->keys[i];
new_values[j] = dict->values[i];
}
}
dict->keys = new_keys;
dict->values = new_values;
dict->capacity = new_capacity;
}
size_t index = ((size_t)key) % dict->capacity;
while (dict->keys[index] != NULL) {
if (dict->keys[index] == key) {
dict->values[index] = value;
return;
}
index = (index + 1) % dict->capacity;
}
dict->keys[index] = key;
dict->values[index] = value;
dict->size++;
}

void* PyDict_GetItem(PyDictObject* dict, void* key) {
size_t index = ((size_t)key) % dict->capacity;
while (dict->keys[index] != NULL) {
if (dict->keys[index] == key) {
return dict->values[index];
}
index = (index + 1) % dict->capacity;
}
return NULL; // Return NULL if key is not found
}
20 changes: 20 additions & 0 deletions runtime/builtin/objects/dictobject.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef DICTOBJECT_H
#define DICTOBJECT_H

#include <stddef.h>
#include "object.h"

typedef struct {
PyObject base;
void** keys;
void** values;
size_t size;
size_t capacity;
} PyDictObject;

unsigned int PyDict_Hash(PyDictObject* dict);
PyDictObject* PyDict_New(size_t initial_capacity);
void PyDict_SetItem(PyDictObject* dict, void* key, void* value);
void* PyDict_GetItem(PyDictObject* dict, void* key);

#endif // DICTOBJECT_H
Loading

0 comments on commit 7852b9e

Please sign in to comment.