Files
2026-10-17 21:57:03 +08:00

104 lines
3.0 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from stdint import *
import stdio
import stdlib
# ===== Pointer operator full-form self-check (TPC side) =====
# `*p = v` targets UnaryOp(Deref); without a dispatch branch it is silently dropped.
def MakePair() -> tuple[int, int]:
return 1, 2
def main() -> int:
fails: int = 0
buf: uint64* = (uint64*)stdlib.malloc(64)
if buf == None:
stdio.printf("FAIL: malloc\n")
return 9
# Form A: *p = v
*buf = 7
if buf[0] != 7:
stdio.printf("FAIL A *p = v : buf[0]=%llu(期望 7)\n", buf[0])
fails += 1
# Form B: *p = *q
q: uint64* = buf + 1
*q = 99
*buf = *q
if buf[0] != 99:
stdio.printf("FAIL B *p = *q : buf[0]=%llu(期望 99)\n", buf[0])
fails += 1
# Form C: &x address-of, *px = v
x: uint64 = 0
y: uint64 = 5
px: uint64* = &x
*px = 11
if x != 11:
stdio.printf("FAIL C *px = v : x=%llu(期望 11)\n", x)
fails += 1
# Form D: *&x = *&y
*&x = *&y
if x != 5:
stdio.printf("FAIL D *&x = *&y : x=%llu(期望 5)\n", x)
fails += 1
# Form E (control): subscript write p[i] = v
buf[0] = 123
if buf[0] != 123:
stdio.printf("FAIL E p[0] = v : buf[0]=%llu(期望 123)\n", buf[0])
fails += 1
# Form F: AugAssign target *p += v
*buf += 5
if buf[0] != 128:
stdio.printf("FAIL F *p += v : buf[0]=%llu(期望 128)\n", buf[0])
fails += 1
# Form G1 (control): tuple target a, b = f()
a: int = 0
b: int = 0
a, b = MakePair()
if a != 1 or b != 2:
stdio.printf("FAIL G1 a, b = f() : a=%d b=%d(期望 1/2)\n", a, b)
fails += 1
# Form G2: list target [c, d] = f()
cc: int = 0
dd: int = 0
[cc, dd] = MakePair()
if cc != 1 or dd != 2:
stdio.printf("FAIL G2 [c, d] = f() : c=%d d=%d(期望 1/2)\n", cc, dd)
fails += 1
# Form H: *p in value position (call arg f(*p)) -- Starred(Load)
got: int = stdio.printf("INFO H f(*p) : %llu(期望 128)\n", *buf)
if got < 0:
stdio.printf("FAIL H f(*p) : printf 返回 %d\n", got)
fails += 1
# Form I: the two-step block-header read memhub uses (key regression point).
# `*((...))` inside t.CPtr() args parses as Starred(Load); old TPC returned 0.
w: uint64 = 0
pw: uint64* = &w
*pw = 0x1234
blk: rawptr = rawptr(uint64(pw))
hd1: uint64 = *((uint64*)(blk))
hd2: uint64 = uint64(rawptr(*((uint64*)(blk))))
if hd1 != 0x1234:
stdio.printf("FAIL I1 直接读块头 : hd1=0x%llX(期望 0x1234)\n", hd1)
fails += 1
if hd2 != 0x1234:
stdio.printf("FAIL I2 两步式读块头 : hd2=0x%llX(期望 0x1234)\n", hd2)
fails += 1
if fails == 0:
stdio.printf("ALL OK: 指针操作符全形态均真正落盘\n")
else:
stdio.printf("FAILS=%d\n", fails)
stdio.fflush(None)
return fails