Skip to content

Commit 9b47781

Browse files
authored
Add a clang-format configuration and reformat C code (riscv#261)
* Add a clang-format configuration and reformat C code From my testing it turns out the built-in WebKit style is the closest to the current style. I added a few config options to further reduce the diff and I think the current output looks reasonable. In the future it would be good to add a CI and pre-commit check to enforce that all C code is consistently formatted to reduce the need for reviewers to look for formatting issues. * Fix formatting of commented-out reservation debug code Use an empty-by-default macro instead of commented-out fprintf calls. This way clang-format can format the calls sensibly and it's easier to enable the debug prints. * Improve formatting of fprintf call in set_config_print() Clang-format does not like long string literals, so split this manually to format the call sensibly. * Fix formatting of function pointer typedef Clang-format gets this wrong `*` is part of the typedef. * Improve formatting of getopt_long call * Fix odd fprintf continuation by splitting long string literal
1 parent 2d21413 commit 9b47781

11 files changed

+413
-236
lines changed

.clang-format

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Language: Cpp
2+
BasedOnStyle: WebKit
3+
AlignArrayOfStructures: Left
4+
AlignAfterOpenBracket: Align
5+
AllowShortFunctionsOnASingleLine: Empty
6+
ColumnLimit: 80
7+
Cpp11BracedListStyle: true
8+
IndentWidth: 2
9+
PointerAlignment: Right
10+
SortIncludes: Never
11+
AlignTrailingComments: true

c_emulator/SoftFloat-3e/.clang-format

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# This is third-party code and should not be reformatted
2+
DisableFormat: true
3+
SortIncludes: Never

c_emulator/riscv_platform.c

+68-27
Original file line numberDiff line numberDiff line change
@@ -4,70 +4,112 @@
44
#include "riscv_platform_impl.h"
55
#include "riscv_sail.h"
66

7+
#ifdef DEBUG_RESERVATION
8+
#include <stdio.h>
9+
#include <inttypes.h>
10+
#define RESERVATION_DBG(args...) fprintf(stderr, args)
11+
#else
12+
#define RESERVATION_DBG(args...)
13+
#endif
14+
715
/* This file contains the definitions of the C externs of Sail model. */
816

917
static mach_bits reservation = 0;
1018
static bool reservation_valid = false;
1119

1220
bool sys_enable_rvc(unit u)
13-
{ return rv_enable_rvc; }
21+
{
22+
return rv_enable_rvc;
23+
}
1424

1525
bool sys_enable_next(unit u)
16-
{ return rv_enable_next; }
26+
{
27+
return rv_enable_next;
28+
}
1729

1830
bool sys_enable_fdext(unit u)
19-
{ return rv_enable_fdext; }
31+
{
32+
return rv_enable_fdext;
33+
}
2034

2135
bool sys_enable_zfinx(unit u)
22-
{ return rv_enable_zfinx; }
36+
{
37+
return rv_enable_zfinx;
38+
}
2339

2440
bool sys_enable_writable_misa(unit u)
25-
{ return rv_enable_writable_misa; }
41+
{
42+
return rv_enable_writable_misa;
43+
}
2644

2745
bool plat_enable_dirty_update(unit u)
28-
{ return rv_enable_dirty_update; }
46+
{
47+
return rv_enable_dirty_update;
48+
}
2949

3050
bool plat_enable_misaligned_access(unit u)
31-
{ return rv_enable_misaligned; }
51+
{
52+
return rv_enable_misaligned;
53+
}
3254

3355
bool plat_mtval_has_illegal_inst_bits(unit u)
34-
{ return rv_mtval_has_illegal_inst_bits; }
56+
{
57+
return rv_mtval_has_illegal_inst_bits;
58+
}
3559

3660
bool plat_enable_pmp(unit u)
37-
{ return rv_enable_pmp; }
61+
{
62+
return rv_enable_pmp;
63+
}
3864

3965
mach_bits plat_ram_base(unit u)
40-
{ return rv_ram_base; }
66+
{
67+
return rv_ram_base;
68+
}
4169

4270
mach_bits plat_ram_size(unit u)
43-
{ return rv_ram_size; }
71+
{
72+
return rv_ram_size;
73+
}
4474

4575
mach_bits plat_rom_base(unit u)
46-
{ return rv_rom_base; }
76+
{
77+
return rv_rom_base;
78+
}
4779

4880
mach_bits plat_rom_size(unit u)
49-
{ return rv_rom_size; }
81+
{
82+
return rv_rom_size;
83+
}
5084

5185
// Provides entropy for the scalar cryptography extension.
5286
mach_bits plat_get_16_random_bits()
53-
{ return rv_16_random_bits(); }
87+
{
88+
return rv_16_random_bits();
89+
}
5490

5591
mach_bits plat_clint_base(unit u)
56-
{ return rv_clint_base; }
92+
{
93+
return rv_clint_base;
94+
}
5795

5896
mach_bits plat_clint_size(unit u)
59-
{ return rv_clint_size; }
97+
{
98+
return rv_clint_size;
99+
}
60100

61101
unit load_reservation(mach_bits addr)
62102
{
63103
reservation = addr;
64104
reservation_valid = true;
65-
/* fprintf(stderr, "reservation <- %0" PRIx64 "\n", reservation); */
105+
RESERVATION_DBG("reservation <- %0" PRIx64 "\n", reservation);
66106
return UNIT;
67107
}
68108

69109
bool speculate_conditional(unit u)
70-
{ return true; }
110+
{
111+
return true;
112+
}
71113

72114
static mach_bits check_mask(void)
73115
{
@@ -78,28 +120,27 @@ bool match_reservation(mach_bits addr)
78120
{
79121
mach_bits mask = check_mask();
80122
bool ret = reservation_valid && (reservation & mask) == (addr & mask);
81-
/*
82-
fprintf(stderr, "reservation(%c): %0" PRIx64 ", key=%0" PRIx64 ": %s\n",
83-
reservation_valid ? 'v' : 'i', reservation, addr, ret ? "ok" : "fail");
84-
*/
85-
123+
RESERVATION_DBG("reservation(%c): %0" PRIx64 ", key=%0" PRIx64 ": %s\n",
124+
reservation_valid ? 'v' : 'i', reservation, addr,
125+
ret ? "ok" : "fail");
86126
return ret;
87127
}
88128

89129
unit cancel_reservation(unit u)
90-
{ /* fprintf(stderr, "reservation <- none\n"); */
130+
{
131+
RESERVATION_DBG("reservation <- none\n");
91132
reservation_valid = false;
92133
return UNIT;
93134
}
94135

95136
unit plat_term_write(mach_bits s)
96-
{ char c = s & 0xff;
137+
{
138+
char c = s & 0xff;
97139
plat_term_write_impl(c);
98140
return UNIT;
99141
}
100142

101-
void plat_insns_per_tick(sail_int *rop, unit u)
102-
{ }
143+
void plat_insns_per_tick(sail_int *rop, unit u) { }
103144

104145
mach_bits plat_htif_tohost(unit u)
105146
{

c_emulator/riscv_platform.h

-1
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,3 @@ unit plat_term_write(mach_bits);
3636
mach_bits plat_htif_tohost(unit);
3737

3838
unit memea(mach_bits, sail_int);
39-

c_emulator/riscv_platform_impl.c

+11-10
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
#include <stdio.h>
44

55
/* Settings of the platform implementation, with common defaults. */
6-
bool rv_enable_pmp = false;
7-
bool rv_enable_zfinx = false;
8-
bool rv_enable_rvc = true;
9-
bool rv_enable_next = false;
10-
bool rv_enable_writable_misa = true;
11-
bool rv_enable_fdext = true;
12-
13-
bool rv_enable_dirty_update = false;
14-
bool rv_enable_misaligned = false;
6+
bool rv_enable_pmp = false;
7+
bool rv_enable_zfinx = false;
8+
bool rv_enable_rvc = true;
9+
bool rv_enable_next = false;
10+
bool rv_enable_writable_misa = true;
11+
bool rv_enable_fdext = true;
12+
13+
bool rv_enable_dirty_update = false;
14+
bool rv_enable_misaligned = false;
1515
bool rv_mtval_has_illegal_inst_bits = false;
1616

1717
uint64_t rv_ram_base = UINT64_C(0x80000000);
@@ -21,7 +21,8 @@ uint64_t rv_rom_base = UINT64_C(0x1000);
2121
uint64_t rv_rom_size = UINT64_C(0x100);
2222

2323
// Provides entropy for the scalar cryptography extension.
24-
uint64_t rv_16_random_bits(void) {
24+
uint64_t rv_16_random_bits(void)
25+
{
2526
// This function can be changed to support deterministic sequences of
2627
// pseudo-random bytes. This is useful for testing.
2728
const char *name = "/dev/urandom";

c_emulator/riscv_platform_impl.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
/* Settings of the platform implementation. */
77

8-
#define DEFAULT_RSTVEC 0x00001000
8+
#define DEFAULT_RSTVEC 0x00001000
99

1010
extern bool rv_enable_pmp;
1111
extern bool rv_enable_zfinx;

c_emulator/riscv_prelude.c

+8-4
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,29 @@ unit print_string(sail_string prefix, sail_string msg)
99

1010
unit print_instr(sail_string s)
1111
{
12-
if (config_print_instr) printf("%s\n", s);
12+
if (config_print_instr)
13+
printf("%s\n", s);
1314
return UNIT;
1415
}
1516

1617
unit print_reg(sail_string s)
1718
{
18-
if (config_print_reg) printf("%s\n", s);
19+
if (config_print_reg)
20+
printf("%s\n", s);
1921
return UNIT;
2022
}
2123

2224
unit print_mem_access(sail_string s)
2325
{
24-
if (config_print_mem_access) printf("%s\n", s);
26+
if (config_print_mem_access)
27+
printf("%s\n", s);
2528
return UNIT;
2629
}
2730

2831
unit print_platform(sail_string s)
2932
{
30-
if (config_print_platform) printf("%s\n", s);
33+
if (config_print_platform)
34+
printf("%s\n", s);
3135
return UNIT;
3236
}
3337

c_emulator/riscv_sail.h

+12-10
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ typedef int unit;
66
#define UNIT 0
77
typedef uint64_t mach_bits;
88

9-
struct zMisa {mach_bits zMisa_chunk_0;};
9+
struct zMisa {
10+
mach_bits zMisa_chunk_0;
11+
};
1012
extern struct zMisa zmisa;
1113

1214
void model_init(void);
@@ -17,9 +19,9 @@ bool zstep(sail_int);
1719
unit ztick_clock(unit);
1820
unit ztick_platform(unit);
1921

20-
unit z_set_Misa_C(struct zMisa*, mach_bits);
21-
unit z_set_Misa_D(struct zMisa*, mach_bits);
22-
unit z_set_Misa_F(struct zMisa*, mach_bits);
22+
unit z_set_Misa_C(struct zMisa *, mach_bits);
23+
unit z_set_Misa_D(struct zMisa *, mach_bits);
24+
unit z_set_Misa_F(struct zMisa *, mach_bits);
2325

2426
#ifdef RVFI_DII
2527
unit zext_rvfi_init(unit);
@@ -54,19 +56,19 @@ extern uint32_t zcur_privilege;
5456

5557
extern mach_bits zPC;
5658

57-
extern mach_bits
58-
zx1, zx2, zx3, zx4, zx5, zx6, zx7,
59-
zx8, zx9, zx10, zx11, zx12, zx13, zx14, zx15,
60-
zx16, zx17, zx18, zx19, zx20, zx21, zx22, zx23,
61-
zx24, zx25, zx26, zx27, zx28, zx29, zx30, zx31;
59+
extern mach_bits zx1, zx2, zx3, zx4, zx5, zx6, zx7, zx8, zx9, zx10, zx11, zx12,
60+
zx13, zx14, zx15, zx16, zx17, zx18, zx19, zx20, zx21, zx22, zx23, zx24,
61+
zx25, zx26, zx27, zx28, zx29, zx30, zx31;
6262

6363
extern mach_bits zmstatus;
6464
extern mach_bits zmepc, zmtval;
6565
extern mach_bits zsepc, zstval;
6666

6767
extern mach_bits zfloat_result, zfloat_fflags;
6868

69-
struct zMcause {mach_bits zMcause_chunk_0;};
69+
struct zMcause {
70+
mach_bits zMcause_chunk_0;
71+
};
7072
extern struct zMcause zmcause, zscause;
7173

7274
extern mach_bits zminstret;

0 commit comments

Comments
 (0)