Skip to content

Commit 277eaa0

Browse files
authored
chore: better UNREACHABLE macro
Use builtins for declaring the code as unreacahable: - On Windows: `__assume(0)` - On Unixes: `__builtin_unreachable()`
1 parent 26a4354 commit 277eaa0

File tree

2 files changed

+5
-6
lines changed

2 files changed

+5
-6
lines changed

src/implementation/c/compilation.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,7 @@ export class Compilation {
166166
out.push(`case ${name}:`);
167167
out.push(`${LABEL_PREFIX}${name}: {`);
168168
lines.forEach((line) => out.push(` ${line}`));
169-
out.push(' /* UNREACHABLE */;');
170-
out.push(' abort();');
169+
out.push(' UNREACHABLE;');
171170
out.push('}');
172171
});
173172
}
@@ -179,8 +178,7 @@ export class Compilation {
179178
}
180179
out.push(`${LABEL_PREFIX}${name}: {`);
181180
lines.forEach((line) => out.push(` ${line}`));
182-
out.push(' /* UNREACHABLE */;');
183-
out.push(' abort();');
181+
out.push(' UNREACHABLE;');
184182
out.push('}');
185183
});
186184
}

src/implementation/c/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,10 @@ export class CCompiler {
6161

6262
out.push('#ifdef _MSC_VER');
6363
out.push(' #define ALIGN(n) _declspec(align(n))');
64+
out.push(' #define UNREACHABLE __assume(0)');
6465
out.push('#else /* !_MSC_VER */');
6566
out.push(' #define ALIGN(n) __attribute__((aligned(n)))');
67+
out.push(' #define UNREACHABLE __builtin_unreachable()');
6668
out.push('#endif /* _MSC_VER */');
6769

6870
out.push('');
@@ -103,8 +105,7 @@ export class CCompiler {
103105
compilation.indent(out, tmp, ' ');
104106

105107
out.push(' default:');
106-
out.push(' /* UNREACHABLE */');
107-
out.push(' abort();');
108+
out.push(' UNREACHABLE;');
108109
out.push(' }');
109110

110111
tmp = [];

0 commit comments

Comments
 (0)