Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
repos:
- repo: https://github.com/psf/black
rev: 20.8b1
rev: 22.6.0
hooks:
- id: black

- repo: https://gitlab.com/pycqa/flake8
rev: 3.9.1
rev: 3.9.2
hooks:
- id: flake8
9 changes: 8 additions & 1 deletion lcc/src/bytecode.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "c.h"
#define I(f) b_##f

static int dump_stack = 0;

static void I(segment)(int n) {
static int cseg;
Expand Down Expand Up @@ -219,9 +220,15 @@ static void I(local)(Symbol p) {
p->x.name = stringf("%d", offset);
p->x.offset = offset;
offset += p->type->size;
if (dump_stack) print("local %s %s %f\n", p->x.name, p->name, p->ref);
}

static void I(progbeg)(int argc, char *argv[]) {}
static void I(progbeg)(int argc, char *argv[]) {
int i;
for (i = 0; i < argc; i++) {
if (!strcmp(argv[i], "-dump-stack")) dump_stack = 1;
}
}

static void I(progend)(void) {}

Expand Down
9 changes: 7 additions & 2 deletions quatch/_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,18 @@
import os
import shutil
import subprocess
from typing import Iterable, Optional
from typing import Iterable, List, Optional


class CompilerError(Exception):
pass


def compile_c_file(
input_path: str, output_path: str, include_dirs: Optional[Iterable[str]] = None
input_path: str,
output_path: str,
include_dirs: Optional[Iterable[str]] = None,
additional_args: Optional[List[str]] = None,
) -> str:
"""Compile C code into lcc bytecode.

Expand All @@ -54,6 +57,8 @@ def compile_c_file(
"-Wf-target=bytecode",
"-Wf-g",
]
if additional_args is not None:
command += additional_args
if include_dirs is not None:
command += [f"-I{include_dir}" for include_dir in include_dirs]
command += ["-o", output_path, input_path]
Expand Down
16 changes: 15 additions & 1 deletion quatch/_instruction.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,16 @@ class Instruction:
Instruction(Opcode.CONST, 0x7b)
"""

def __init__(self, opcode: Opcode, operand: Optional[Operand] = None) -> None:
def __init__(
self,
opcode: Opcode,
operand: Optional[Operand] = None,
debug_info: Optional[str] = "",
) -> None:
"""Initialize an Instruction from an opcode and operand."""
self._opcode: Opcode = opcode
self._operand: Optional[Operand] = None
self.debug_info = debug_info

if operand is not None:
self.operand = operand
Expand All @@ -160,6 +166,14 @@ def __str__(self) -> str:
else:
return f"{self._opcode.name} {self._operand:#x}"

def __eq__(self, other: Instruction) -> bool:
def normalize(x):
return (x & 0xFFFFFFFF) if type(x) == int and x < 0 else x

return self.opcode == other.opcode and normalize(self._operand) == normalize(
other._operand
)

@property
def opcode(self) -> Opcode:
return self._opcode
Expand Down
48 changes: 14 additions & 34 deletions quatch/_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,28 +52,21 @@ def __getitem__(self, key):
if isinstance(key, int):
key = self._check_index(key)
region = self.region_at(key)
if region is None or region.contents is None:
if region.contents is None:
return 0
else:
return region.contents[key - region.begin]

elif isinstance(key, slice):
key = self._check_slice(key)
result = bytearray()
position = key.start
for region in self.regions_overlapping(key.start, key.stop):
# gaps caused by align() should be filled with zeros
result.extend(b"\x00" * (region.begin - position))
position = region.end

begin = max(0, key.start - region.begin)
end = region.size - max(0, (region.end - key.stop))
if region.contents is None:
result.extend(b"\x00" * (end - begin))
else:
result.extend(region.contents[begin:end])

result.extend(b"\x00" * (key.stop - position))
return result

else:
Expand All @@ -96,8 +89,9 @@ def __setitem__(self, key, value):
if isinstance(key, int):
key = self._check_index(key)
region = self.region_at(key)
if region is None or region.contents is None:
raise IndexError("cannot assign to padding or BSS")
if region.contents is None:
if value != 0:
raise ValueError("cannot assign nonzero data to BSS")
else:
region.contents[key - region.begin] = value

Expand All @@ -109,33 +103,17 @@ def __setitem__(self, key, value):
if key.stop <= key.start:
return

regions = self.regions_overlapping(key.start, key.stop)
writable = True

# check for gaps between key.start and key.stop
position = key.start
for region in regions:
if region.begin > position or region.contents is None:
break
position = region.end

if position < key.stop:
writable = False

# if there were no regions found and the slice isn't empty then the whole
# thing is padding
if len(regions) == 0 and key.start < key.stop:
writable = False

if not writable:
raise IndexError("cannot assign to padding or BSS")

for region in regions:
for region in self.regions_overlapping(key.start, key.stop):
src_begin = max(0, region.begin - key.start)
src_end = min(len(value), region.end - key.start)
dst_begin = max(0, key.start - region.begin)
dst_end = min(region.size, key.stop - region.begin)
region.contents[dst_begin:dst_end] = value[src_begin:src_end]
data = value[src_begin:src_end]
if region.tag == RegionTag.BSS:
if not all(byte == 0 for byte in data):
raise ValueError("cannot assign nonzero data to BSS")
else:
region.contents[dst_begin:dst_end] = data

else:
raise TypeError("indices must be integers or slices")
Expand Down Expand Up @@ -213,7 +191,9 @@ def align(self, alignment: int) -> None:

Does nothing if len(self) is already a multiple of alignment.
"""
self._size = align(self._size, alignment)
padding_size = align(self._size, alignment) - self._size
if padding_size != 0:
self.add_region(RegionTag.BSS, size=padding_size)

def regions_with_tag(self, tag: RegionTag) -> Iterator[Region]:
"""Find all regions with a given tag."""
Expand Down
Loading