Skip to content

Commit 4606c47

Browse files
authored
Merge pull request #105 from kulp/misc-cleanup
Collect miscellaneous cleanups/fixes
2 parents 7f3097b + 172a0e7 commit 4606c47

File tree

16 files changed

+77
-45
lines changed

16 files changed

+77
-45
lines changed

.github/workflows/c.yml

+3-13
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@ jobs:
2121
uses: Homebrew/actions/setup-homebrew@master
2222
- run: brew install bison expect icarus-verilog lcov
2323
- if: env.SDL != 0
24-
run: |
25-
brew install sdl2
26-
# See below for notes about `--HEAD` here.
27-
brew install --HEAD sdl2_image
24+
run: brew install sdl2 sdl2_image
2825
- uses: actions/checkout@v2
2926
with:
3027
submodules: true
@@ -43,26 +40,19 @@ jobs:
4340
matrix:
4441
os:
4542
- ubuntu-latest
46-
- macos-10.15
4743
- macos-latest
4844
runs-on: ${{ matrix.os }}
4945
steps:
5046
- name: Set up Homebrew
5147
id: set-up-homebrew
5248
uses: Homebrew/actions/setup-homebrew@master
49+
- run: brew upgrade || true # Avoid failures due to incomplete links.
5350
- run: brew install bison expect icarus-verilog
5451
- if: runner.os == 'macOS'
5552
run: echo "/usr/local/opt/bison/bin" >> $GITHUB_PATH
5653
- if: env.SDL != 0
5754
run: |
58-
brew install sdl2
59-
# In SDL2_image 2.6.0, CMake support was added, but as of the current
60-
# release 2.6.1, the Homebrew build does not yet work due to prefix
61-
# computation that was fixed in
62-
# https://github.com/libsdl-org/SDL_image/pull/295
63-
# TODO stop installing from HEAD when a package with a fix (SDL2_image
64-
# 2.6.2 perhaps?) is released.
65-
brew install --HEAD sdl2_image
55+
brew install sdl2 sdl2_image
6656
- uses: actions/checkout@v2
6757
with:
6858
submodules: true

COPYING

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (C) 2011-2022 Darren Kulp
1+
Copyright (C) 2011-2023 Darren Kulp
22
Portions Copyright (C) 2012 Forest Belton
33

44
Permission is hereby granted, free of charge, to any person obtaining a copy of

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ all:
1818
check: all
1919
cmake -S . -B build -DJIT=${JIT} -DSDL=${SDL} -DICARUS=${ICARUS} -DTESTING=1
2020
cmake --build build
21-
export PATH=$(abspath .):$$PATH && cd build && ctest --rerun-failed --output-on-failure
21+
export PATH=$(abspath .):$$PATH && cd build && ctest --output-on-failure
2222

2323
# Use CMAKE_BUILD_TYPE=Debug for coverage to avoid glitchy optimizations.
2424
coverage: CMAKE_BUILD_TYPE = Debug

hw/verilog/top.v

+11-11
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ module Tenyr(
5252
.INIT(0),
5353
.PBITS(32), .ABITS(RAMABITS), .OFFSET(`RESETVECTOR)
5454
) ram(
55-
.clka ( clk_core ), .clkb ( '0 ),
56-
.ena ( r_stb ), .enb ( '0 ),
57-
.acka ( r_ack ), .ackb ( ),
58-
.wea ( r_wen ), .web ( '0 ),
59-
.addra ( r_adr ), .addrb ( '0 ),
60-
.dina ( r_ddn ), .dinb ( '0 ),
61-
.douta ( r_dup ), .doutb ( )
55+
.clka ( clk_core ), .clkb ( 1'b0 ),
56+
.ena ( r_stb ), .enb ( 1'b0 ),
57+
.acka ( r_ack ), .ackb ( ),
58+
.wea ( r_wen ), .web ( 1'b0 ),
59+
.addra ( r_adr ), .addrb ( 32'h0 ),
60+
.dina ( r_ddn ), .dinb ( 32'h0 ),
61+
.douta ( r_dup ), .doutb ( )
6262
);
6363

6464
// -----------------------------------------------------------------------------
@@ -154,10 +154,10 @@ module Tenyr(
154154
.wbs_cyc_o ({ o_cyc, g_cyc, v_cyc, s_cyc, r_cyc, x_cyc }),
155155

156156
// unused ports
157-
.wbm_cti_i ( '0 ),
158-
.wbm_bte_i ( '0 ),
159-
.wbs_cti_o ( ),
160-
.wbs_bte_o ( )
157+
.wbm_cti_i ( 3'h0 ),
158+
.wbm_bte_i ( 2'h0 ),
159+
.wbs_cti_o ( ),
160+
.wbs_bte_o ( )
161161
);
162162

163163
endmodule

src/asm.c

+35-9
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ static int text_in(STREAM *stream, struct element *i, void *ud)
471471
char next_char = 0;
472472
size_t len = stream->op.fread(&next_char, 1, 1, stream);
473473
if (len > 0 && !isspace(next_char))
474-
return -1;
474+
fatal(0, "Invalid character '%c' (ordinal %#hhx)", next_char, next_char);
475475
return result ? 1 : -1;
476476
}
477477

@@ -491,8 +491,7 @@ static int text_out(STREAM *stream, struct element *i, void *ud)
491491
*/
492492

493493
struct memh_state {
494-
int32_t written, marked, offset;
495-
int emit_zeros;
494+
int32_t seen, written, marked, offset;
496495
bool first_done;
497496
};
498497

@@ -505,8 +504,6 @@ static int memh_init(STREAM *stream, struct param_state *p, void **ud)
505504
param_get_int(p, "format.memh.offset", &state->offset);
506505
state->marked = state->written = state->offset;
507506

508-
param_get_int(p, "format.memh.explicit", &state->emit_zeros);
509-
510507
return 0;
511508
}
512509

@@ -522,9 +519,12 @@ static int memh_in(STREAM *stream, struct element *i, void *ud)
522519
if (stream->op.fscanf(stream, " @%x", &state->marked) == 1)
523520
return 0; // let next call handle it
524521

525-
if (stream->op.fscanf(stream, " %x", &i->insn.u.word) != 1)
522+
if (stream->op.feof(stream))
526523
return -1;
527524

525+
if (stream->op.fscanf(stream, " %x", &i->insn.u.word) != 1)
526+
fatal(0, "Failed to read hex digits");
527+
528528
i->insn.reladdr = state->written++;
529529
state->marked = state->written;
530530
return 1;
@@ -542,7 +542,13 @@ static int memh_out(STREAM *stream, struct element *i, void *ud)
542542
int32_t word = i->insn.u.word;
543543
int32_t diff = addr - state->written;
544544

545-
if (word == 0 && !state->emit_zeros)
545+
// `seen` refers to the last index seen, inclusive -- therefore we need to
546+
// subtract one after adding the size. In the normal case, size will be 1,
547+
// so `seen` will point at `addr`. In the case of a zero-sized element,
548+
// seen will become the element right *before* `addr`.
549+
state->seen = addr + i->insn.size - 1;
550+
551+
if (word == 0)
546552
return 0; // 0 indicates success but nothing was output
547553

548554
state->written = addr;
@@ -555,6 +561,25 @@ static int memh_out(STREAM *stream, struct element *i, void *ud)
555561
return (printed + stream->op.fprintf(stream, "%08x\n", word) > 3) ? 1 : -1;
556562
}
557563

564+
static int memh_fini(STREAM *stream, void **ud)
565+
{
566+
const struct memh_state *state = *ud;
567+
const int32_t addr = state->seen;
568+
const int32_t diff = addr - state->written;
569+
570+
// There may be unprinted zeros.
571+
if (diff > 0)
572+
{
573+
if (stream->op.fprintf(stream, "@%x ", addr) < 3)
574+
fatal(0, "Failed to emit address adjustment");
575+
576+
if (stream->op.fprintf(stream, "%08x\n", 0) < 8)
577+
fatal(0, "Failed to emit hex value");
578+
}
579+
580+
return gen_fini(stream, ud);
581+
}
582+
558583
const struct format tenyr_asm_formats[] = {
559584
// first format is default
560585
{ "obj",
@@ -574,10 +599,11 @@ const struct format tenyr_asm_formats[] = {
574599
.init = memh_init,
575600
.in = memh_in,
576601
.out = memh_out,
577-
.fini = gen_fini },
602+
.fini = memh_fini },
578603
};
579604

580-
const size_t tenyr_asm_formats_count = countof(tenyr_asm_formats);
605+
const size_t tenyr_asm_formats_count =
606+
(sizeof tenyr_asm_formats) / (sizeof tenyr_asm_formats[0]);
581607

582608
size_t make_format_list(int (*pred)(const struct format *), size_t flen,
583609
const struct format *fmts, size_t len, char *buf, const char *sep)

src/common.h

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
#include "ops.h"
1010

11-
#define countof(X) (sizeof (X) / sizeof (X)[0])
1211
#define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
1312

1413
static inline int32_t SEXTEND32(unsigned int bits, int32_t val)

src/os/default/findself.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
typedef int path_walker(size_t len, const char *name, void *ud);
1212
// Returns walker's last result (stops walking when walker returns true), or
1313
// returns 0 if PATH is empty or missing.
14-
int os_walk_path_search_list(path_walker walker, void *ud)
14+
static int os_walk_path_search_list(path_walker walker, void *ud)
1515
{
1616
int found = 0;
1717

src/plugin.c

+5-2
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,16 @@ int plugin_load(const char *basepath, const char **paths, const char *base,
2626
{
2727
int rc = 0;
2828

29+
const int max_impls = 16;
30+
// We use a constant `16` here, instead of `max_impls`, to avoid making
31+
// `impls` a VLA (at least under some compilers) and thus making it illegal
32+
// to initialize it with an initializer list.
2933
const void *impls[16] = { 0 };
3034
int inst = 0;
3135
do {
3236
char buf[256] = { 0 }, parent[256] = { 0 };
3337
snprintf(parent, sizeof parent, "%s[%d]", base, inst);
34-
int count = p->gops.param_get(p, parent, countof(impls), impls);
35-
const int max_impls = (signed)countof(impls);
38+
int count = p->gops.param_get(p, parent, max_impls, impls);
3639
if (count > max_impls)
3740
debug(0, "Saw %d plugins names, handling only the first %d", count, max_impls);
3841
else if (count <= 0)

src/tas.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ int main(int argc, char *argv[])
204204
if (disassemble)
205205
flags |= ASM_DISASSEMBLE;
206206

207-
for (int i = optind; i < argc; i++) {
207+
for (int i = optind; rc == 0 && i < argc; i++) {
208208
rc = process_file(params, flags, fmt, argv[i], out);
209209
// We may have created an output file already, but we do not try to
210210
// remove it, because doing so by filename would be a race condition.

src/tsim.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ static int add_recipe(struct sim_state *s, const char *name)
309309
#define Entry(Name,Desc) { STR(Name), recipe_##Name },
310310
RECIPES(Entry)
311311
};
312-
lfind_size_t sz = countof(entries);
312+
lfind_size_t sz = (sizeof entries) / (sizeof entries[0]);
313313

314314
struct recipe_entry *r = lfind(&(struct recipe_entry){ .name = name },
315315
entries, &sz, sizeof entries[0],

test/compare/tas/err/memh_explicit

Whitespace-only changes.

test/compare/tas/in/memh_explicit

-1
This file was deleted.

test/compare/tas/out/memh_explicit

-2
This file was deleted.

test/misc/memh/agda.memh

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
agda

test/pass_compile/zeros_only.tas

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.zero 2
2+
.zero 0
3+
.zero (2 + 3)
4+
.zero 0

tests.cmake

+13-1
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,6 @@ set_tests_properties(
342342
check_std_outputs(NAME "text disassembly" COMMAND ${CMAKE_TENYR_COMPILER} ARGS -ftext -d)
343343
check_std_outputs(NAME "quiet disassembly" COMMAND ${CMAKE_TENYR_COMPILER} ARGS -ftext -d -q)
344344
check_std_outputs(NAME "verbose disassembly" COMMAND ${CMAKE_TENYR_COMPILER} ARGS -ftext -d -v)
345-
check_std_outputs(NAME "memh explicit" COMMAND ${CMAKE_TENYR_COMPILER} ARGS -fmemh -pformat.memh.explicit=1)
346345
check_std_outputs(NAME "memh offset" COMMAND ${CMAKE_TENYR_COMPILER} ARGS -fmemh -pformat.memh.offset=5)
347346
check_std_outputs(NAME "params overflow" COMMAND ${CMAKE_TENYR_COMPILER} ARGS -fmemh -pA=1 -pB=1 -pC=1 -pD=1 -pE=1 -pF=1 -pG=1 -pH=1 -pI=1 -pJ=1 -pK=1 -pL=1 -pM=1 -pN=1 -pO=1 -pP=1 -pQ=1 -pR=1 -pS=1 -pT=1 -pU=1 -pV=1 -pW=1 -pX=1 -pY=1 -pZ=1 -pformat.memh.offset=5)
348347

@@ -633,3 +632,16 @@ check_failure(
633632
)
634633

635634

635+
check_failure(
636+
NAME "invalid input for text"
637+
COMMAND ${CMAKE_TENYR_COMPILER}
638+
ARGS --format=text -d ${CMAKE_SOURCE_DIR}/test/misc/memh/agda.memh
639+
EXPECT "Invalid character"
640+
)
641+
642+
check_failure(
643+
NAME "invalid input for memh"
644+
COMMAND ${CMAKE_TENYR_COMPILER}
645+
ARGS --format=memh -d ${CMAKE_SOURCE_DIR}/test/misc/memh/agda.memh
646+
EXPECT "Failed to read hex digits"
647+
)

0 commit comments

Comments
 (0)