Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ Disassembly of section .text:
$ size main.o
```

```bash
```text
text data bss dec hex filename
103 0 0 103 67 main.o
```
Expand All @@ -110,7 +110,7 @@ Disassembly of section .text:
$ size main.o
```

```bash
```text
text data bss dec hex filename
90 0 0 90 5a main.o
```
Expand All @@ -137,7 +137,7 @@ Disassembly of section .text:
$ size main.o
```

```bash
```text
text data bss dec hex filename
87 0 0 87 57 main.o
```
Expand All @@ -154,7 +154,7 @@ $ rm -f (path filter *.o); clang -O1 -c main.c; llvm-objdump -d --x86-asm-syntax
```

```bash
rm -f (path filter *.o); clang -O1 -c main.c; llvm-objdump -d --x86-asm-syntax=att main.o
$ rm -f (path filter *.o); clang -O1 -c main.c; llvm-objdump -d --x86-asm-syntax=att main.o
```

```x86asm
Expand Down Expand Up @@ -217,7 +217,7 @@ int main() {
$ rm -f (path filter *.out); clang -g -O2 -o app.out main.c; lldb app.out
```

```bash
```text
(lldb) target create "app.out"
Current executable set to '/home/gapry/Workspaces/test/app.out' (x86_64).
(lldb) breakpoint set -n main
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ Hence, the compiler needs to use two instructions to execute the addition at the

## Optimized Analysis
```bash
rm -f (path filter *.o); clang -O2 -c add.c; llvm-objdump -d --x86-asm-syntax=att add.o
$ rm -f (path filter *.o); clang -O2 -c add.c; llvm-objdump -d --x86-asm-syntax=att add.o
```

```x86asm
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ The instruction `sub sp, sp, #0x20` performs Stack Allocation by decrementing th
The subsequent `stp` (Store Pair) instruction implements Context Saving, pushing the Frame Pointer (`X29`) and Link Register (`X30`) onto the stack to
facilitate the Stack Unwinding process during the Function Epilogue.

```bash
```text
| Higher Address |
| |
+----------------+ <--- Previous SP
Expand Down Expand Up @@ -229,7 +229,7 @@ In the unoptimized execution, each recursive call of `add(x + 1, y - 1)` trigger
resulting in the allocation of a new activation record.
This manifests as `O(n)` space complexity, as the stack grows linearly with the input value of `y`.

```bash
```text
| Higher Address |
+--------------------+
| add(1, 10) Frame | (Initial Call: a = 1, b = 10)
Expand Down Expand Up @@ -265,7 +265,7 @@ as no further operations are required after the callee returns.
This reduces the space complexity from `O(n)` to `O(1)` and eliminates the overhead associated
with stack frame allocation and deallocation.

```bash
```text
| Higher Address | | Higher Address | | Higher Address |
+--------------------+ +-------------------+ +-------------------+ <-- Previous SP
| add(1, 10) Frame | -> | add(2, 9) Frame | -> ... -> | add(11, 0) Frame | (Reused for all steps)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ The compiler defaults to the `mul` instruction because decomposing the constant
If the compiler were to adopt a manual shift-and-subtract strategy,
the code generator would need to output three instructions:
```armasm
add w8, w0, w0, lsl 1 // w8 = x + 2x = 3x
add w8, w0, w0, lsl #1 // w8 = x + 2x = 3x
lsl w8, w8, #2 // w8 = w8 << 2 = 3x << 2 = 3x * 4 = 12x
sub w0, w8, w0 // w0 = 12x - x = 11x
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ Disassembly of section .text:
```

Instruction:
```text
```x86asm
shrl <imm>, <Rd> ; shr := Shift Right Logical, that is <Rd> = <Rd> >> <imm>
```

Expand Down Expand Up @@ -208,7 +208,7 @@ cmp <Wn>, #imm ; Compares w0 with #0x0, and update the processor
csel <Wd>, <Wn>, <Wm>, <cond> ; Conditional Select.
; If the condition lt (Less Than) is true, it selects w8;
; otherwise, it selects w0.
asr <Wd>, <Wn>, #imm : Arithmetic Shift Right, w0 = w8 >> 9
asr <Wd>, <Wn>, #imm ; Arithmetic Shift Right, w0 = w8 >> 9
```

| Flag | Name | Bit | Description (when set to 1) |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ unsigned div10(unsigned x) {
By disassembling the compiled object file, we can observe the magic number $M$ and the shift operation.

```bash
clang -O2 -c main.c; llvm-objdump -d --disassemble-symbols=div10 --x86-asm-syntax=att main.o
$ clang -O2 -c main.c; llvm-objdump -d --disassemble-symbols=div10 --x86-asm-syntax=att main.o
```

```x86asm
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ $ g++ -O2 -S sum1.cpp -o sum1.s
$ llvm-mca -march=x86-64 -mcpu=x86-64 -timeline sum1.s
```

```bash
```text
[0] Code Region - MyLoop

Iterations: 100
Expand Down Expand Up @@ -173,7 +173,7 @@ $ clang++ -O2 -S sum1.cpp -o sum1.s
$ llvm-mca -march=x86-64 -mcpu=x86-64 -timeline sum1.s
```

```bash
```text
[0] Code Region - MyLoop

Iterations: 100
Expand Down Expand Up @@ -284,7 +284,7 @@ $ g++ -O2 -S sum2.cpp -o sum2.s
$ llvm-mca -march=x86-64 -mcpu=x86-64 -timeline sum2.s
```

```bash
```text
[0] Code Region - MyLoop

Iterations: 100
Expand Down Expand Up @@ -364,7 +364,7 @@ $ clang++ -O2 -S sum2.cpp -o sum2.s
$ llvm-mca -march=x86-64 -mcpu=x86-64 -timeline sum2.s
```

```bash
```text
sum2.s:67:3: error: found an invalid region end directive
# LLVM-MCA-END
^
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ $ clang++ -std=c++20 -O2 -fno-unroll-loops -fno-vectorize -mno-sse -mno-avx -c s
$ llvm-objdump -d --disassemble-symbols=$(nm sum.o | awk '/sum/ {print $3}') --x86-asm-syntax=intel sum.o
```

```txt
```x86asm
sum.o: file format elf64-x86-64

Disassembly of section .text:
Expand Down Expand Up @@ -150,7 +150,7 @@ $ clang++ -std=c++20 -O2 -fno-unroll-loops -fno-vectorize -mno-sse -mno-avx -c s
$ llvm-objdump -d --disassemble-symbols=$(nm sum.o | awk '/sum/ {print $3}') --x86-asm-syntax=intel sum.o
```

```text
```x86asm
sum.o: file format elf64-x86-64

Disassembly of section .text:
Expand Down Expand Up @@ -195,7 +195,7 @@ $ clang++ -std=c++20 -O2 -fno-vectorize -mno-sse -mno-avx -c sum.cpp
$ llvm-objdump -d --disassemble-symbols=$(nm sum.o | awk '/sum/ {print $3}') --x86-asm-syntax=intel sum.o
```

```text
```x86asm
sum.o: file format elf64-x86-64

Disassembly of section .text:
Expand Down Expand Up @@ -245,7 +245,7 @@ $ clang++ -std=c++20 -O2 -fno-vectorize -mno-sse -mno-avx -c sum.cpp
$ radare2 -q -e bin.cache=true -c "aa; pdf" sum.o
```

```text
```radare2
;-- section..text:
;-- .text:
;-- reloc..text:
Expand Down Expand Up @@ -295,7 +295,7 @@ $ clang++ -std=c++20 -O2 -fno-vectorize -mno-sse -mno-avx -c sum.cpp
$ radare2 -q -e bin.cache=true -c "aa; pdf" sum.o
```

```text
```radare2
;-- section..text:
;-- .text:
;-- reloc..text:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ $ rm -f app.out && clang -O2 -o app.out main.c
$ llvm-objdump -d --disassemble-symbols=count_set_bits --x86-asm-syntax=att app.out
```

```text
```x86asm
0000000000001140 <count_set_bits>:
1140: 31 c0 xorl %eax, %eax
1142: 85 ff testl %edi, %edi
Expand All @@ -81,7 +81,7 @@ $ rm -f app.out && gcc -O2 -o app.out main.c
$ llvm-objdump -d --disassemble-symbols=count_set_bits --x86-asm-syntax=att app.out
```

```text
```x86asm
0000000000001180 <count_set_bits>:
1180: f3 0f 1e fa endbr64
1184: 31 c0 xorl %eax, %eax
Expand Down Expand Up @@ -134,7 +134,7 @@ $ rm -f app.out && clang -O2 -mpopcnt -o app.out main.c
$ llvm-objdump -d --disassemble-symbols=count_set_bits --x86-asm-syntax=att app.out
```

```text
```x86asm
0000000000001140 <count_set_bits>:
1140: f3 0f b8 c7 popcntl %edi, %eax
1144: c3 retq
Expand All @@ -146,7 +146,7 @@ $ rm -f app.out && gcc -O2 -mpopcnt -o app.out main.c
$ llvm-objdump -d --disassemble-symbols=count_set_bits --x86-asm-syntax=att app.out
```

```text
```x86asm
0000000000001180 <count_set_bits>:
1180: f3 0f 1e fa endbr64
1184: 31 c0 xorl %eax, %eax
Expand Down
24 changes: 24 additions & 0 deletions src/components/MarkdownRenderer/MarkdownRenderer.css
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,30 @@
display: none;
}

.markdown-body .ansi-green {
color: var(--color-ansi-green);
}

.markdown-body .ansi-red {
color: var(--color-ansi-red);
}

.markdown-body .ansi-yellow {
color: var(--color-ansi-yellow);
}

.markdown-body .ansi-blue {
color: var(--color-ansi-blue);
}

.markdown-body .ansi-magenta {
color: var(--color-ansi-magenta);
}

.markdown-body .ansi-cyan {
color: var(--color-ansi-cyan);
}

.katex {
color: var(--color-text);
font-size: 1.1em;
Expand Down
37 changes: 12 additions & 25 deletions src/components/MarkdownRenderer/MarkdownRenderer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,20 @@ import remarkGfm from 'remark-gfm';
import remarkMath from 'remark-math';
import rehypeKatex from 'rehype-katex';
import 'katex/dist/katex.min.css';
import { Prism as PrismHighlighter } from 'react-syntax-highlighter';
import { Light as HLJSHighlighter } from 'react-syntax-highlighter';
import { Prism as PrismHighlighter, PrismLight } from 'react-syntax-highlighter';

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛑 Logic Error: PrismLight and PrismHighlighter are two separate instances — custom languages registered on PrismLight are not available to PrismHighlighter. The PrismCodeBlock component uses PrismHighlighter for rendering, so it will never find x86asm, armasm-objdump, or radare2, causing those blocks to fall back to plain text. Either register languages on PrismHighlighter directly, or switch the renderer component to PrismLight as well.

Suggested change
import { Prism as PrismHighlighter, PrismLight } from 'react-syntax-highlighter';
import { PrismLight as PrismHighlighter } from 'react-syntax-highlighter';

import { vscDarkPlus as PrismStyles } from 'react-syntax-highlighter/dist/esm/styles/prism';
import { darcula as HLJSStyles } from 'react-syntax-highlighter/dist/esm/styles/hljs';
import x86asm from './prism/x86asm';
import armasm from './prism/armasm';
import radare2 from './prism/radare2';
import './MarkdownRenderer.css'

import x86asm from 'react-syntax-highlighter/dist/esm/languages/hljs/x86asm';
import armasm from 'react-syntax-highlighter/dist/esm/languages/hljs/armasm';
PrismLight.registerLanguage(x86asm.displayName, x86asm);
PrismLight.registerLanguage(armasm.displayName, armasm);
PrismLight.registerLanguage(radare2.displayName, radare2);

HLJSHighlighter.registerLanguage('x86asm', x86asm);
HLJSHighlighter.registerLanguage('armasm', armasm);

const HLJS_LANGS = []; // ['x86asm', 'armasm'];

const HLJSCodeBlock = ({ language, content, ...props }) => (
<HLJSHighlighter
style={HLJSStyles}
language={language}
PreTag="div"
{...props}
>
{content}
</HLJSHighlighter>
);
const LANGUAGE_OVERRIDES = {
armasm: armasm.displayName,
};

const PrismCodeBlock = ({ language, content, ...props }) => (
<PrismHighlighter
Expand All @@ -43,13 +33,10 @@ const CodeBlock = ({ inline, className, children, ...props }) => {
const match = /language-(\w+)/.exec(className || '');

if (!inline && match) {
const language = match[1];
const language = LANGUAGE_OVERRIDES[match[1]] || match[1];
const content = String(children).replace(/\n$/, '');

const isHLJS = HLJS_LANGS.includes(language);
const Component = isHLJS ? HLJSCodeBlock : PrismCodeBlock;

return <Component language={language} content={content} {...props} />;
return <PrismCodeBlock language={language} content={content} {...props} />;
}

return (
Expand Down
23 changes: 23 additions & 0 deletions src/components/MarkdownRenderer/prism/armasm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export default function armasm(Prism) {
Prism.languages[armasm.displayName] = {
'asm-comment': /(?:\/\/|;).*/,
'asm-string': /"(?:\\.|[^"\\\n])*"/,
'asm-symbol': /<[^<>\n]+>/,
'asm-immediate': {
pattern: /#-?(?:0x[\da-f]+|\d+(?:\.\d+)?(?:e[+-]?\d+)?)/i,
alias: 'ansi-red',
},
'asm-register': {
pattern: /\b(?:[wx](?:[12]?\d|30|zr)|w?sp|[vqdshb](?:[12]?\d|3[01])(?:\.\d*[bhsdq])?)\b(?!:)/,
alias: 'ansi-cyan',
},
'asm-address': {
pattern: /(^|[^\w+\-.#])0x[\da-f]+\b/i,
lookbehind: true,
alias: 'ansi-yellow',
},
};
}

armasm.displayName = 'armasm-objdump';
armasm.aliases = [];
Loading
Loading