Skip to content

Commit

Permalink
updatellvmintro
Browse files Browse the repository at this point in the history
  • Loading branch information
archibate committed Aug 22, 2024
1 parent f9863f7 commit 16f8572
Show file tree
Hide file tree
Showing 8 changed files with 394 additions and 35 deletions.
12 changes: 6 additions & 6 deletions docs/extra.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ for (var i = 0; i < links.length; i++) {
}
}

var llvms = document.querySelectorAll('code.language-llvm');
for (var i = 0; i < llvms.length; i++) {
var llvm = llvms[i];
var nasms = document.querySelectorAll('code.language-nasm');
for (var i = 0; i < nasms.length; i++) {
var nasm = nasms[i];
// add class .language-wasm .hljs
llvm.classList.add('language-wasm', 'hljs');
// remove class .language-llvm
llvm.classList.remove('language-llvm');
nasm.classList.add('language-x86asm', 'hljs');
// remove class .language-nasm
nasm.classList.remove('language-nasm');
}

var stylesheets = [
Expand Down
Binary file added docs/img/llvm-factorial-instructions.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/img/llvm-module.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/img/llvm-register-lifespan.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
328 changes: 302 additions & 26 deletions docs/llvm_intro.md

Large diffs are not rendered by default.

73 changes: 71 additions & 2 deletions docs/undef.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ void func(){
```cpp
int i;
float f = *(float *)&i; // 错!
*(int *)(uintptr_t)&i; // 可以
```

例外:char、signed char、unsigned char 和 std::byte 总是兼容任何类型
Expand All @@ -121,6 +120,31 @@ char *buf = (char *)&i; // 可以
buf[0] = 1; // 可以
```

> uint8_t 是 unsigned char 的别名,所以也兼容任何类型
例外:int 和 unsigned int 互相兼容

```cpp
int i;
unsigned int f = *(unsigned int *)&i; // 可以
```

例外:const int * 和 int * 互相兼容(二级指针强转)

```cpp
const int *cp;
int *p = *(int **)&cp; // 可以
```

注意:只取决于访问时的类型是否正确,中间可以转换为别的类型(如 void * 和 uintptr_t),只需最后访问时转换回正确的指针类型即可

```cpp
int i;
*(int *)(uintptr_t)&i; // 可以
*(int *)(void *)&i; // 可以
*(int *)(float *)&i; // 可以
```

### union 访问不是激活的成员

```cpp
Expand Down Expand Up @@ -199,6 +223,51 @@ char buf[sizeof(int) * 2];
int *p = (int *)(((uintptr_t)buf + sizeof(int) - 1) & ~(alignof(int) - 1)); // 可以
```

### 从父类 static_cast 到不符合的子类后访问

```cpp
struct Base {};
struct Derived : Base {};

Base b;
Derived d = *(Derived *)&b; // 错!
Derived d = *static_cast<Derived *>(&b); // 错!
Derived d = static_cast<Derived &>(b); // 错!
```
```cpp
Derived obj;
Base *bp = &obj;
Derived d = *(Derived *)bp; // 可以
Derived d = *static_cast<Derived *>(bp); // 可以
Derived d = static_cast<Derived &>(*bp); // 可以
```

### bool 类型不得出现 0 和 1 以外的值

布尔类型 bool,只有 true 和 false 两种取值。

bool 虽然占据 1 字节(8 位)内存空间,但其中只有一个有效位,也就是最低位。

只有这个最低位可以是 0 或 1,其余 7 位必须始终保持为 0。

如果其余位中出现了非 0 的位,也就是出现 0 和 1 以外的取值,则是未定义行为。

```cpp
char c = 0;
bool b = *(bool *)c; // 可以,b = false
```

```cpp
char c = 1;
bool b = *(bool *)c; // 可以,b = true
```

```cpp
char c = 2;
bool b = *(bool *)c; // 未定义行为
```

## 算数类

### 有符号整数的加减乘除模不能溢出
Expand Down Expand Up @@ -431,7 +500,7 @@ int func(int x) {
但也有例外:
1. `main` 函数可以不写 `return` 语句,默认自带 `return 0;`
1. 主函数 `main` 可以不写 `return` 语句,默认自带 `return 0;`
2. 协程函数可以不写 `return` 语句,如果有 `co_return` 或者协程返回类型为 `void` 且具有至少一个 `co_await` 出现
### 函数指针被调用时,不能为空
Expand Down
2 changes: 1 addition & 1 deletion misc/typst/cppguidebook.typ
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ C++ 源码 `.cpp` 是写给人类看的!计算机并不认识,计算机只
=== MSVC
```cmd
```bash
cl.exe /c main.cpp
```
Expand Down
14 changes: 14 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ theme:
features:
- search.suggest
- search.highlight
highlightjs: true
hljs_languages: # rg '^```\w+' | sed 's/.*:```\(\w*\)/ - \1/g' | sort | uniq
- bash
- c
- cmake
- cpp
- diff
- java
- json
- llvm
- nasm
- python
- rust
- txt
extra_javascript:
- extra.js
- https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-AMS-MML_HTMLorMML
Expand Down

0 comments on commit 16f8572

Please sign in to comment.