Skip to content

Commit 6003411

Browse files
authored
Merge branch 'sysprog21:master' into fix_est
2 parents 390a0b6 + fcfe649 commit 6003411

File tree

11 files changed

+691
-171
lines changed

11 files changed

+691
-171
lines changed

.ci/check-format.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ set -x
66

77
for file in ${SOURCES};
88
do
9-
clang-format-12 ${file} > expected-format
9+
clang-format-18 ${file} > expected-format
1010
diff -u -p --label="${file}" --label="expected coding style" ${file} expected-format
1111
done
12-
exit $(clang-format-12 --output-replacements-xml ${SOURCES} | egrep -c "</replacement>")
12+
exit $(clang-format-18 --output-replacements-xml ${SOURCES} | egrep -c "</replacement>")

.github/workflows/main.yml

+6-5
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ on: [push, pull_request]
44

55
jobs:
66
host-x86:
7-
runs-on: ubuntu-22.04
7+
runs-on: ubuntu-24.04
88
strategy:
99
matrix:
10-
compiler: [gcc-12, clang]
10+
compiler: [gcc, clang]
1111
architecture: [arm, riscv]
1212
steps:
1313
- name: checkout code
@@ -25,7 +25,7 @@ jobs:
2525
make check || exit 1
2626
2727
host-arm:
28-
runs-on: ubuntu-22.04
28+
runs-on: ubuntu-24.04
2929
steps:
3030
- name: checkout code
3131
uses: actions/checkout@v4
@@ -37,6 +37,7 @@ jobs:
3737
arch: none
3838
distro: none
3939
base_image: "--platform=linux/arm/v7 arm32v7/ubuntu:22.04"
40+
githubToken: ${{ github.token }}
4041
install: |
4142
apt-get update -q -y
4243
apt-get install -q -y build-essential
@@ -45,12 +46,12 @@ jobs:
4546
make check || exit 1
4647
4748
coding-style:
48-
runs-on: ubuntu-22.04
49+
runs-on: ubuntu-24.04
4950
steps:
5051
- uses: actions/checkout@v4
5152
- name: coding convention
5253
run: |
53-
sudo apt-get install -q -y clang-format-12
54+
sudo apt-get install -q -y clang-format-18
5455
.ci/check-newline.sh
5556
.ci/check-format.sh
5657
shell: bash

CONTRIBUTING.md

+524-61
Large diffs are not rendered by default.

lib/c.c

+52-89
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
#define true 1
1414
#define false 0
1515

16+
#define INT_MAX 0x7fffffff
17+
#define INT_MIN 0x80000000
18+
1619
#if defined(__arm__)
1720
#define __SIZEOF_POINTER__ 4
1821
#define __syscall_exit 1
@@ -288,60 +291,65 @@ int __format(char *buffer,
288291
return bi;
289292
}
290293

291-
void printf(char *str, ...)
294+
int __format_to_buf(char *buffer, char *format, int *var_args, int size)
292295
{
293-
int *var_args = &str + 4;
294-
char buffer[200];
295296
int si = 0, bi = 0, pi = 0;
296297

297-
while (str[si]) {
298-
if (str[si] != '%') {
299-
buffer[bi] = str[si];
298+
if (size == 0)
299+
return 0;
300+
301+
while (format[si] && bi < size - 1) {
302+
if (format[si] != '%') {
303+
buffer[bi] = format[si];
300304
bi++;
301305
si++;
302306
} else {
303-
int w = 0, zp = 0, pp = 0;
307+
int w = 0, zp = 0, pp = 0, v = var_args[pi], l;
304308

305309
si++;
306-
if (str[si] == '#') {
310+
if (format[si] == '#') {
307311
pp = 1;
308312
si++;
309313
}
310-
if (str[si] == '0') {
314+
if (format[si] == '0') {
311315
zp = 1;
312316
si++;
313317
}
314-
if (str[si] >= '1' && str[si] <= '9') {
315-
w = str[si] - '0';
318+
if (format[si] >= '1' && format[si] <= '9') {
319+
w = format[si] - '0';
316320
si++;
317-
while (str[si] >= '0' && str[si] <= '9') {
321+
while (format[si] >= '0' && format[si] <= '9') {
318322
w *= 10;
319-
w += str[si] - '0';
323+
w += format[si] - '0';
320324
si++;
321325
}
322326
}
323-
if (str[si] == 's') {
327+
switch (format[si]) {
328+
case 's':
324329
/* append param pi as string */
325-
int l = strlen(var_args[pi]);
326-
strcpy(buffer + bi, var_args[pi]);
330+
l = strlen(v);
331+
l = l < size - bi ? l : size - bi;
332+
strncpy(buffer + bi, v, l);
327333
bi += l;
328-
} else if (str[si] == 'c') {
334+
break;
335+
case 'c':
329336
/* append param pi as char */
330-
buffer[bi] = var_args[pi];
337+
buffer[bi] = v;
331338
bi += 1;
332-
} else if (str[si] == 'o') {
339+
break;
340+
case 'o':
333341
/* append param as octal */
334-
int v = var_args[pi];
335342
bi += __format(buffer + bi, v, w, zp, 8, pp);
336-
} else if (str[si] == 'd') {
343+
break;
344+
case 'd':
337345
/* append param as decimal */
338-
int v = var_args[pi];
339346
bi += __format(buffer + bi, v, w, zp, 10, 0);
340-
} else if (str[si] == 'x') {
347+
break;
348+
case 'x':
341349
/* append param as hex */
342-
int v = var_args[pi];
343350
bi += __format(buffer + bi, v, w, zp, 16, pp);
344-
} else if (str[si] == '%') {
351+
break;
352+
case '%':
345353
/* append literal '%' character */
346354
buffer[bi] = '%';
347355
bi++;
@@ -352,71 +360,27 @@ void printf(char *str, ...)
352360
si++;
353361
}
354362
}
355-
buffer[bi] = 0;
356-
__syscall(__syscall_write, 1, buffer, bi);
363+
364+
int len = size - 1 > bi ? bi : size - 1;
365+
buffer[len] = 0;
366+
return len;
357367
}
358368

359-
void sprintf(char *buffer, char *str, ...)
369+
int printf(char *str, ...)
360370
{
361-
int *var_args = &str + 4;
362-
int si = 0, bi = 0, pi = 0;
371+
char buffer[200];
372+
int len = __format_to_buf(buffer, str, &str + 4, INT_MAX);
373+
return __syscall(__syscall_write, 1, buffer, len);
374+
}
363375

364-
while (str[si]) {
365-
if (str[si] != '%') {
366-
buffer[bi] = str[si];
367-
bi++;
368-
si++;
369-
} else {
370-
int w = 0, zp = 0, pp = 0;
376+
int sprintf(char *buffer, char *str, ...)
377+
{
378+
return __format_to_buf(buffer, str, &str + 4, INT_MAX);
379+
}
371380

372-
si++;
373-
if (str[si] == '#') {
374-
pp = 1;
375-
si++;
376-
}
377-
if (str[si] == '0') {
378-
zp = 1;
379-
si++;
380-
}
381-
if (str[si] >= '1' && str[si] <= '9') {
382-
w = str[si] - '0';
383-
si++;
384-
if (str[si] >= '0' && str[si] <= '9') {
385-
w *= 10;
386-
w += str[si] - '0';
387-
si++;
388-
}
389-
}
390-
switch (str[si]) {
391-
case 37: /* % */
392-
buffer[bi++] = '%';
393-
si++;
394-
continue;
395-
case 99: /* c */
396-
buffer[bi++] = var_args[pi];
397-
break;
398-
case 115: /* s */
399-
strcpy(buffer + bi, var_args[pi]);
400-
bi += strlen(var_args[pi]);
401-
break;
402-
case 111: /* o */
403-
bi += __format(buffer + bi, var_args[pi], w, zp, 8, pp);
404-
break;
405-
case 100: /* d */
406-
bi += __format(buffer + bi, var_args[pi], w, zp, 10, 0);
407-
break;
408-
case 120: /* x */
409-
bi += __format(buffer + bi, var_args[pi], w, zp, 16, pp);
410-
break;
411-
default:
412-
abort();
413-
break;
414-
}
415-
pi++;
416-
si++;
417-
}
418-
}
419-
buffer[bi] = 0;
381+
int snprintf(char *buffer, int n, char *str, ...)
382+
{
383+
return __format_to_buf(buffer, str, &str + 4, n);
420384
}
421385

422386
int __free_all();
@@ -496,10 +460,9 @@ char *fgets(char *str, int n, FILE *stream)
496460

497461
int fputc(int c, FILE *stream)
498462
{
499-
char buf[1];
500-
buf[0] = c;
501-
__syscall(__syscall_write, stream, buf, 1);
502-
return 0;
463+
if (__syscall(__syscall_write, stream, &c, 1) < 0)
464+
return -1;
465+
return c;
503466
}
504467

505468
/* Non-portable: Assume page size is 4KiB */

src/defs.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@
1717
#define MAX_VAR_LEN 32
1818
#define MAX_TYPE_LEN 32
1919
#define MAX_PARAMS 8
20-
#define MAX_LOCALS 1500
20+
#define MAX_LOCALS 1600
2121
#define MAX_FIELDS 64
2222
#define MAX_FUNCS 512
2323
#define MAX_TYPES 64
24-
#define MAX_IR_INSTR 50000
24+
#define MAX_IR_INSTR 60000
2525
#define MAX_BB_PRED 128
2626
#define MAX_BB_DOM_SUCC 64
2727
#define MAX_BB_RDOM_SUCC 256
2828
#define MAX_GLOBAL_IR 256
2929
#define MAX_LABEL 4096
30-
#define MAX_SOURCE 327680
30+
#define MAX_SOURCE 524288
3131
#define MAX_CODE 262144
3232
#define MAX_DATA 262144
3333
#define MAX_SYMTAB 65536
@@ -39,7 +39,7 @@
3939
#define MAX_CASES 128
4040
#define MAX_NESTING 128
4141
#define MAX_OPERAND_STACK_SIZE 32
42-
#define MAX_ANALYSIS_STACK_SIZE 750
42+
#define MAX_ANALYSIS_STACK_SIZE 800
4343

4444
/* Default capacities for common data structures */
4545
/* Default arena size is initialized with 256 KiB */

src/parser.c

+7-6
Original file line numberDiff line numberDiff line change
@@ -2621,6 +2621,9 @@ basic_block_t *read_body_statement(block_t *parent, basic_block_t *bb)
26212621
if (lex_peek(T_numeric, NULL)) {
26222622
case_val = read_numeric_constant(token_str);
26232623
lex_expect(T_numeric); /* already read it */
2624+
} else if (lex_peek(T_char, token)) {
2625+
case_val = token[0];
2626+
lex_expect(T_char);
26242627
} else {
26252628
constant_t *cd = find_constant(token_str);
26262629
case_val = cd->value;
@@ -3401,17 +3404,15 @@ void load_source_file(char *file)
34013404
}
34023405
if (!strncmp(buffer, "#include ", 9) && (buffer[9] == '"')) {
34033406
char path[MAX_LINE_LEN];
3404-
int c = strlen(file) - 1;
3407+
int c = strlen(file) - 1, inclusion_path_len = strlen(buffer) - 11;
34053408
while (c > 0 && file[c] != '/')
34063409
c--;
34073410
if (c) {
34083411
/* prepend directory name */
3409-
strncpy(path, file, c + 1);
3410-
c++;
3412+
snprintf(path, c + 2, "%s", file);
34113413
}
3412-
path[c] = 0;
3413-
buffer[strlen(buffer) - 2] = 0;
3414-
strcpy(path + c, buffer + 10);
3414+
3415+
snprintf(path + c + 1, inclusion_path_len, "%s", buffer + 10);
34153416
load_source_file(path);
34163417
} else {
34173418
strcpy(SOURCE->elements + SOURCE->size, buffer);

0 commit comments

Comments
 (0)