Docs for views and the INT rule: tutorial, raz(7), tour, an example; Math by views
| @@ -195,6 +195,13 @@ | ||
| 195 | 195 | f(x)`, a `for`'s bounds, `INC`/`DEC`'s step, and where it meets an |
| 196 | 196 | `I64` in an operator (report 2026-10.26, 5.10 and 6.1). Never to a |
| 197 | 197 | `U64`, nor an `I64` back to an `INT`. |
| 198 | +- A view, `v.(T)`, is `v`'s item with `T` for its type: nothing is | |
| 199 | + copied. A variable kept in a register goes to memory for it, as for | |
| 200 | + `ADR`. Another module's variable may be viewed, and not written. A | |
| 201 | + view that runs past its end fails as kind 7 of `Rt.fail`. `src/`, | |
| 202 | + and the library it imports (`Str`, `Bytes`, ...), use neither views | |
| 203 | + nor the widening of 2026-10.26, since the seed builds them, until a | |
| 204 | + release makes a new seed. | |
| 198 | 205 | - An `extern` module variable is read-only for an importer, as the |
| 199 | 206 | report says of every exported variable (3). |
| 200 | 207 | - Warnings come only for a module with no errors. A procedure naming |
| @@ -19,7 +19,7 @@ | ||
| 19 | 19 | | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes | |
| 20 | 20 | | `src/X64.raz` | 1,517 | 1,253 | The instruction encoder, jumps within a procedure, the assembler | |
| 21 | 21 | | `src/Gen.raz` | 2,552 | 1,821 | The stack machine, for x86-64: values, operations, checks, calls, frames | |
| 22 | -| `src/Item.raz` | 1,948 | 1,594 | Items: types, constants, variables, operators, conversions, calls | | |
| 22 | +| `src/Item.raz` | 1,954 | 1,598 | Items: types, constants, variables, operators, conversions, calls | | |
| 23 | 23 | | `src/Parse.raz` | 4,000 | 3,478 | Declarations, statements, expressions, built-ins, recovery, warnings | |
| 24 | 24 | | `src/Link.raz` | 381 | 298 | Tree shaking, layout, patching, the places | |
| 25 | 25 | | `src/Elf.raz` | 431 | 344 | Static and dynamic ELF, symbol table, line numbers | |
| @@ -184,20 +184,32 @@ | ||
| 184 | 184 | Arithmetic wraps at the type's width, and never stops the program, |
| 185 | 185 | except dividing by zero. |
| 186 | 186 | .Pp |
| 187 | -Two types never mix: with | |
| 188 | -.Cm "i: I64" , | |
| 189 | -.Cm "i < LEN(s)" | |
| 187 | +Two types never mix but for one: | |
| 188 | +.Cm INT | |
| 189 | +into | |
| 190 | +.Cm I64 , | |
| 191 | +which loses nothing, wherever the one meets the other, so with | |
| 192 | +.Cm "big: I64" , | |
| 193 | +.Cm "big + n" | |
| 194 | +is an | |
| 195 | +.Cm I64 . | |
| 196 | +With | |
| 197 | +.Cm "c: I32" , | |
| 198 | +.Cm "c < n" | |
| 190 | 199 | is an error. |
| 191 | 200 | A type's name converts, as |
| 192 | 201 | .Cm F64(n) |
| 193 | 202 | and |
| 194 | 203 | .Cm I64(b) |
| 195 | -do above. | |
| 196 | -The one silent conversion between numbers is an | |
| 204 | +do above; an | |
| 205 | +.Cm I64 | |
| 206 | +becomes an | |
| 197 | 207 | .Cm INT |
| 198 | -into an | |
| 199 | -.Cm I64 , | |
| 200 | -which loses nothing. | |
| 208 | +only so, as | |
| 209 | +.Cm INT(big) , | |
| 210 | +since an | |
| 211 | +.Cm INT | |
| 212 | +may be smaller on another machine. | |
| 201 | 213 | A number written in the program is untyped until it meets a type; |
| 202 | 214 | .Cm 0FFH |
| 203 | 215 | is hexadecimal, and |
| @@ -438,8 +450,179 @@ | ||
| 438 | 450 | .Cm b^.lo , |
| 439 | 451 | never |
| 440 | 452 | .Cm b.lo . |
| 441 | -.Sh 7. MEMORY AND ALLOCATORS | |
| 453 | +.Sh 7. BYTES AND VIEWS | |
| 454 | +A file, a network packet or a device hands a program bytes, and the | |
| 455 | +program wants numbers and records out of them. | |
| 456 | +A | |
| 457 | +.Em view | |
| 458 | +reads and writes bytes as another type. | |
| 459 | +.Bd -literal -offset indent | |
| 460 | +module Packet; | |
| 461 | +import Out; | |
| 462 | + | |
| 463 | +type Header = record kind: U16; flags: U16; size: U32; end; | |
| 464 | + | |
| 465 | +var buf: [16]U8; | |
| 466 | + | |
| 467 | +proc main*; | |
| 468 | +var i: INT; n: I64; h: Header; x: F64; | |
| 469 | +begin | |
| 470 | + buf[0].(I64) := 258; (* buf[0] to buf[7] now hold 258 *) | |
| 471 | + for i := 0 to 7 do Out.int(I64(buf[i])); Out.char(' '); end; | |
| 472 | + Out.ln; | |
| 473 | + n := buf[0].(I64); (* and give it back *) | |
| 474 | + Out.int(n); Out.ln; | |
| 475 | + | |
| 476 | + buf[8].(Header) := {kind: 1, size: 4096}; | |
| 477 | + h := buf[8].(Header); | |
| 478 | + Out.int(I64(h.size)); Out.ln; | |
| 479 | + INC(buf[8].(Header).size, 4); | |
| 480 | + Out.int(I64(buf[8].(Header).size)); Out.ln; | |
| 481 | + | |
| 482 | + x := 1.0; | |
| 483 | + Out.hex(x.(U64), 16); Out.ln; (* the bits of 1.0 *) | |
| 484 | + Out.int(I64(x)); Out.ln; (* the number 1.0 *) | |
| 485 | +end; | |
| 486 | + | |
| 487 | +end Packet. | |
| 488 | +.Ed | |
| 489 | +.Bd -literal -offset indent | |
| 490 | +2 1 0 0 0 0 0 0 | |
| 491 | +258 | |
| 492 | +4096 | |
| 493 | +4100 | |
| 494 | +3FF0000000000000 | |
| 495 | +1 | |
| 496 | +.Ed | |
| 497 | +.Pp | |
| 498 | +Read | |
| 499 | +.Cm buf[0].(I64) | |
| 500 | +as | |
| 501 | +.Dq "the I64 whose bytes start at buf[0]" . | |
| 502 | +An | |
| 503 | +.Cm I64 | |
| 504 | +is 8 bytes, so the view is made of | |
| 505 | +.Cm buf[0] | |
| 506 | +to | |
| 507 | +.Cm buf[7] : | |
| 508 | +.Bd -literal -offset indent | |
| 509 | +buf: [0] [1] [2] [3] [4] [5] [6] [7] [8] ... | |
| 510 | + 2 1 0 0 0 0 0 0 | |
| 511 | + \e______ buf[0].(I64) = 258 ______/ | |
| 512 | +.Ed | |
| 513 | +.Pp | |
| 514 | +Nothing is copied to make a view: it | |
| 515 | +.Em is | |
| 516 | +those bytes, as | |
| 517 | +.Cm p^ | |
| 518 | +is the variable | |
| 519 | +.Cm p | |
| 520 | +points at. | |
| 521 | +Assigning it writes them, and so changes | |
| 522 | +.Cm buf ; | |
| 523 | +reading it reads them. | |
| 524 | +So a view goes wherever a variable goes: on the left of | |
| 525 | +.Cm := , | |
| 526 | +in | |
| 527 | +.Cm INC , | |
| 528 | +and with a field or an index after it, as | |
| 529 | +.Cm buf[8].(Header).size | |
| 530 | +is the | |
| 531 | +.Cm size | |
| 532 | +field of the | |
| 533 | +.Cm Header | |
| 534 | +whose bytes start at | |
| 535 | +.Cm buf[8] . | |
| 536 | +.Pp | |
| 537 | +258 is | |
| 538 | +.Cm 102H , | |
| 539 | +and its low byte, 2, comes first: x86-64 puts a number's low byte | |
| 540 | +first in memory. | |
| 541 | +Another machine may put it last; a format with an order of its own is | |
| 542 | +read a byte at a time, with | |
| 543 | +.Cm Str.le | |
| 544 | +or | |
| 545 | +.Cm Str.be , | |
| 546 | +if the program is to run everywhere. | |
| 547 | +.Pp | |
| 548 | +A view isn't a conversion. | |
| 549 | +A conversion works on the value, a view on the bytes: | |
| 550 | +.Cm I64(x) | |
| 551 | +is 1, the number, while | |
| 552 | +.Cm x.(U64) | |
| 553 | +is | |
| 554 | +.Cm 3FF0000000000000H , | |
| 555 | +the bits that hold 1.0. | |
| 556 | +When the program means a number it wants a conversion; when it means | |
| 557 | +the bytes as they lie, a view. | |
| 558 | +.Pp | |
| 559 | +The view must lie inside the variable it is made of. | |
| 560 | +For an element, as | |
| 561 | +.Cm buf[i].(I64) , | |
| 562 | +that is from | |
| 563 | +.Cm buf[i] | |
| 564 | +to the end of | |
| 565 | +.Cm buf , | |
| 566 | +and the program checks it as it checks an index: | |
| 442 | 567 | .Bd -literal -offset indent |
| 568 | +module Overrun; | |
| 569 | +import Out; | |
| 570 | + | |
| 571 | +var buf: [16]U8; | |
| 572 | + | |
| 573 | +proc main*; | |
| 574 | +var i: INT; | |
| 575 | +begin | |
| 576 | + i := 0; | |
| 577 | + while i < 16 do | |
| 578 | + buf[i].(I32) := 1; | |
| 579 | + i := i + 3; | |
| 580 | + end; | |
| 581 | + Out.str("done\en"); | |
| 582 | +end; | |
| 583 | + | |
| 584 | +end Overrun. | |
| 585 | +.Ed | |
| 586 | +.Bd -literal -offset indent | |
| 587 | +Overrun.raz:11:13: a view at index 15 runs past a length of 16 | |
| 588 | + in Overrun.main at Overrun.raz:11:13: i = 15 | |
| 589 | +.Ed | |
| 590 | +.Pp | |
| 591 | +The | |
| 592 | +.Cm I32 | |
| 593 | +at | |
| 594 | +.Cm buf[15] | |
| 595 | +would be | |
| 596 | +.Cm buf[15] | |
| 597 | +to | |
| 598 | +.Cm buf[18] , | |
| 599 | +three bytes past the end. | |
| 600 | +An index the compiler knows, it checks itself: | |
| 601 | +.Cm buf[14].(I32) | |
| 602 | +doesn't compile. | |
| 603 | +A view of a whole variable must fit in it: | |
| 604 | +.Cm x.(U64) | |
| 605 | +needs | |
| 606 | +.Cm x | |
| 607 | +to have 8 bytes at least. | |
| 608 | +.Pp | |
| 609 | +A view is an integer, a real or a pointer, or a record or array made | |
| 610 | +of them: types that any bytes are a value of. | |
| 611 | +A | |
| 612 | +.Cm BOOL | |
| 613 | +or an enumeration isn't, since some bytes are none of its values: | |
| 614 | +view an integer, and compare or convert it. | |
| 615 | +A view may start at any byte, so it has no address: | |
| 616 | +.Cm ADR | |
| 617 | +of it is an error. | |
| 618 | +Copy it to a variable first. | |
| 619 | +Before views, a program made a pointer for each access, as | |
| 620 | +.Cm "p := ^I64(ADR(buf[0]))" | |
| 621 | +and then | |
| 622 | +.Cm "p^ := 258" , | |
| 623 | +which checks nothing. | |
| 624 | +.Sh 8. MEMORY AND ALLOCATORS | |
| 625 | +.Bd -literal -offset indent | |
| 443 | 626 | module Stack; |
| 444 | 627 | import Out, Mem, Heap, Arena; |
| 445 | 628 | |
| @@ -510,7 +693,7 @@ | ||
| 510 | 693 | .Cm Map |
| 511 | 694 | take the allocator they use from |
| 512 | 695 | .Cm init . |
| 513 | -.Sh 8. ERRORS | |
| 696 | +.Sh 9. ERRORS | |
| 514 | 697 | .Bd -literal -offset indent |
| 515 | 698 | module Errors; |
| 516 | 699 | import Out, Str; |
| @@ -607,7 +790,7 @@ | ||
| 607 | 790 | .Cm main , |
| 608 | 791 | which returns none, tests each error and deals with it. |
| 609 | 792 | raz has no exceptions. |
| 610 | -.Sh 9. DEFER | |
| 793 | +.Sh 10. DEFER | |
| 611 | 794 | .Bd -literal -offset indent |
| 612 | 795 | module Defers; |
| 613 | 796 | import Out, Sys; |
| @@ -674,7 +857,7 @@ | ||
| 674 | 857 | was reached. |
| 675 | 858 | So a file is closed beside the line that opens it, however the |
| 676 | 859 | procedure ends. |
| 677 | -.Sh 10. CHECKS, AND WHAT A FAILURE SAYS | |
| 860 | +.Sh 11. CHECKS, AND WHAT A FAILURE SAYS | |
| 678 | 861 | .Bd -literal -offset indent |
| 679 | 862 | module Fail; |
| 680 | 863 | import Out; |
| @@ -723,7 +906,7 @@ | ||
| 723 | 906 | in its environment has it say where its time went; |
| 724 | 907 | .Xr raz 1 |
| 725 | 908 | says more. |
| 726 | -.Sh 11. ENUMERATIONS, SETS, VARIANTS AND MATCH | |
| 909 | +.Sh 12. ENUMERATIONS, SETS, VARIANTS AND MATCH | |
| 727 | 910 | .Bd -literal -offset indent |
| 728 | 911 | module Shapes; |
| 729 | 912 | import Out, Fmt, Math; |
| @@ -816,7 +999,7 @@ | ||
| 816 | 999 | takes constants and ranges, and on a string, string constants; both |
| 817 | 1000 | must have an |
| 818 | 1001 | .Cm "| else:" . |
| 819 | -.Sh 12. GENERIC MODULES | |
| 1002 | +.Sh 13. GENERIC MODULES | |
| 820 | 1003 | A module may take types, and the procedures it needs on them, as |
| 821 | 1004 | parameters. |
| 822 | 1005 | .Pa Biggest.raz : |
| @@ -887,7 +1070,7 @@ | ||
| 887 | 1070 | .Bd -literal -offset indent |
| 888 | 1071 | import Count := Map([]U8, INT, Str.hash, Str.eq); |
| 889 | 1072 | .Ed |
| 890 | -.Sh 13. TALKING TO C | |
| 1073 | +.Sh 14. TALKING TO C | |
| 891 | 1074 | .Bd -literal -offset indent |
| 892 | 1075 | module Qsort; |
| 893 | 1076 | import Out, Fmt; |
| @@ -949,7 +1132,7 @@ | ||
| 949 | 1132 | .Xr printf 3 |
| 950 | 1133 | keep two buffers, so a program that writes with both flushes one |
| 951 | 1134 | before it writes with the other. |
| 952 | -.Sh 14. THREADS | |
| 1135 | +.Sh 15. THREADS | |
| 953 | 1136 | .Bd -literal -offset indent |
| 954 | 1137 | module Hits; |
| 955 | 1138 | import Out, Thread; |
| @@ -1013,7 +1196,7 @@ | ||
| 1013 | 1196 | C; one that does makes its threads with C's |
| 1014 | 1197 | .Xr pthread_create 3 |
| 1015 | 1198 | instead. |
| 1016 | -.Sh 15. FROM PASCAL AND C TO RAZ | |
| 1199 | +.Sh 16. FROM PASCAL AND C TO RAZ | |
| 1017 | 1200 | .Bl -column "for (i=0; i<n; i++)" "for i := 0 to n-1 do" "Mem.free(Heap.heap, ^U8(p))" |
| 1018 | 1201 | .It Sy C Ta Sy Pascal Ta Sy raz |
| 1019 | 1202 | .It "#include" Ta "uses" Ta "import Out, Str;" |
| @@ -252,7 +252,10 @@ | ||
| 252 | 252 | .Cm I64 , |
| 253 | 253 | .Cm UINT |
| 254 | 254 | into |
| 255 | -.Cm U64 | |
| 255 | +.Cm U64 , | |
| 256 | +wherever it goes, results passed on whole and a | |
| 257 | +.Cm for Ns 's | |
| 258 | +bounds too | |
| 256 | 259 | [5.10]. |
| 257 | 260 | Anything else is converted by name. |
| 258 | 261 | .Sh OPERATORS |
| @@ -269,8 +272,15 @@ | ||
| 269 | 272 | .Bl -bullet |
| 270 | 273 | .It |
| 271 | 274 | Both operands have one type, or one is an untyped constant that fits |
| 272 | -the other's: with | |
| 273 | -.Cm "i: I64" , | |
| 275 | +the other's, or one is an | |
| 276 | +.Cm INT | |
| 277 | +and the other an | |
| 278 | +.Cm I64 | |
| 279 | +.Pq Cm UINT , U64 , | |
| 280 | +and the result is an | |
| 281 | +.Cm I64 : | |
| 282 | +with | |
| 283 | +.Cm "i: I32" , | |
| 274 | 284 | .Cm "i < LEN(s)" |
| 275 | 285 | is an error. |
| 276 | 286 | Integers wrap at their width. |
| @@ -344,6 +354,33 @@ | ||
| 344 | 354 | .Cm BOOL |
| 345 | 355 | and integers, and none to records, arrays, slices, variants or |
| 346 | 356 | .Cm ERROR . |
| 357 | +.Sh VIEWS | |
| 358 | +[6.9] | |
| 359 | +.Cm v.(T) | |
| 360 | +is the | |
| 361 | +.Cm T | |
| 362 | +whose bytes start where the variable | |
| 363 | +.Cm v | |
| 364 | +does: a variable, not a copy, read and written. | |
| 365 | +.Bd -literal -offset indent | |
| 366 | +mem[16].(I64) := -1; (* mem[16] to mem[23] *) | |
| 367 | +k := buf[0].(Header).count; | |
| 368 | +bits := x.(U64); (* an F64's bits; I64(x) is its number *) | |
| 369 | +.Ed | |
| 370 | +.Pp | |
| 371 | +.Cm T | |
| 372 | +is an integer, a real or a pointer, or a record or array of them. | |
| 373 | +.Cm "a[i].(T)" | |
| 374 | +lies from | |
| 375 | +.Cm a[i] | |
| 376 | +to the end of | |
| 377 | +.Cm a , | |
| 378 | +which is checked as an index is; any other | |
| 379 | +.Cm v.(T) | |
| 380 | +lies within | |
| 381 | +.Cm v . | |
| 382 | +A view may be misaligned, so it has no address, and a volatile | |
| 383 | +variable can't be viewed. | |
| 347 | 384 | .Sh BUILT-IN PROCEDURES |
| 348 | 385 | [6.8] |
| 349 | 386 | .Bl -tag -width "SYSCALL(n, x1, ...)" |
| @@ -515,6 +552,9 @@ | ||
| 515 | 552 | an index past the length, and a slice out of |
| 516 | 553 | .Cm "0 <= lo <= hi <= length" , |
| 517 | 554 | unless the module has checks off; |
| 555 | +.It | |
| 556 | +a view that runs past the end of what it is made of, with the same | |
| 557 | +proviso; | |
| 518 | 558 | .It |
| 519 | 559 | a slice of a pointer that runs backwards, and a number that is no |
| 520 | 560 | position of the enumeration it converts to; |
| @@ -0,0 +1,2 @@ | ||
| 1 | +class 2, byte order 1, machine 62, header 64 bytes | |
| 2 | +3 segments loaded, 1 of them code |
| @@ -0,0 +1,77 @@ | ||
| 1 | +(* Views: reads the ELF header of the program itself, and its program | |
| 2 | + headers, which say how the system loads it into memory. The file is | |
| 3 | + read whole into bytes; each header is a record laid over them with a | |
| 4 | + view, b[i].(T), which the program checks lies inside the bytes. *) | |
| 5 | +module Elf; | |
| 6 | +import Out, Fmt, Buf, Heap, Files; | |
| 7 | + | |
| 8 | +type | |
| 9 | + (* the file's first 64 bytes, as the ELF specification lays them | |
| 10 | + out: a C struct, so a record lays them out the same way *) | |
| 11 | + Header = record | |
| 12 | + ident: [16]U8; (* 7FH 'E' 'L' 'F', the class, the byte order, ... *) | |
| 13 | + kind: U16; (* 2, an executable; 3, a shared object *) | |
| 14 | + machine: U16; (* 62, x86-64 *) | |
| 15 | + version: U32; | |
| 16 | + entry: U64; (* where the program starts *) | |
| 17 | + phoff: U64; (* where the program headers are in the file *) | |
| 18 | + shoff: U64; | |
| 19 | + flags: U32; | |
| 20 | + ehsize: U16; (* this header's size *) | |
| 21 | + phentsize: U16; (* a program header's size *) | |
| 22 | + phnum: U16; (* how many there are *) | |
| 23 | + shentsize, shnum, shstrndx: U16; | |
| 24 | + end; | |
| 25 | + (* one piece of the file, loaded into memory or telling the loader | |
| 26 | + something *) | |
| 27 | + Segment = record | |
| 28 | + kind: U32; (* 1, loaded *) | |
| 29 | + flags: U32; (* 1, executable; 2, writable; 4, readable *) | |
| 30 | + offset, vaddr, paddr, filesz, memsz, align: U64; | |
| 31 | + end; | |
| 32 | + | |
| 33 | +(* "r-x" and the like, for flags *) | |
| 34 | +proc rights(flags: U32): [3]U8; | |
| 35 | +var r: [3]U8; | |
| 36 | +begin | |
| 37 | + r := "---"; | |
| 38 | + if flags & 4 # 0 then r[0] := 'r'; end; | |
| 39 | + if flags & 2 # 0 then r[1] := 'w'; end; | |
| 40 | + if flags & 1 # 0 then r[2] := 'x'; end; | |
| 41 | + return r; | |
| 42 | +end; | |
| 43 | + | |
| 44 | +proc main*; | |
| 45 | +var b: Buf.Buf; f: []U8; h: Header; i, at, loaded, exec: INT; r: [3]U8; | |
| 46 | +begin | |
| 47 | + Buf.init(ADR(b), Heap.heap); | |
| 48 | + defer Buf.done(ADR(b)); | |
| 49 | + if not Files.read("/proc/self/exe", ADR(b)) then | |
| 50 | + Out.str("can't read the program\n"); | |
| 51 | + return; | |
| 52 | + end; | |
| 53 | + f := Buf.bytes(ADR(b)); | |
| 54 | + | |
| 55 | + (* the header, copied out of the bytes whole *) | |
| 56 | + h := f[0].(Header); | |
| 57 | + if h.ident[0..4] # "\x7FELF" then Out.str("not ELF\n"); return; end; | |
| 58 | + Fmt.print("class {}, byte order {}, machine {}, header {} bytes\n", | |
| 59 | + [Fmt.int(I64(h.ident[4])), Fmt.int(I64(h.ident[5])), Fmt.int(I64(h.machine)), | |
| 60 | + Fmt.int(I64(h.ehsize))]); | |
| 61 | + if h.phentsize # SIZE(Segment) then Out.str("odd program headers\n"); return; end; | |
| 62 | + | |
| 63 | + (* each program header, read in place: no copy of the record *) | |
| 64 | + loaded := 0; | |
| 65 | + exec := 0; | |
| 66 | + for i := 0 to INT(h.phnum) - 1 do | |
| 67 | + at := INT(h.phoff) + i * SIZE(Segment); | |
| 68 | + if f[at].(Segment).kind = 1 then | |
| 69 | + INC(loaded); | |
| 70 | + r := rights(f[at].(Segment).flags); | |
| 71 | + if r = "r-x" then INC(exec); end; | |
| 72 | + end; | |
| 73 | + end; | |
| 74 | + Fmt.print("{} segments loaded, {} of them code\n", [Fmt.int(loaded), Fmt.int(exec)]); | |
| 75 | +end; | |
| 76 | + | |
| 77 | +end Elf. |
| @@ -15,19 +15,16 @@ | ||
| 15 | 15 | ln2hi = 6.93147180369123816490E-01; (* ln 2 in two parts, whose sum is more exact than one F64 *) |
| 16 | 16 | ln2lo = 1.90821492927058770002E-10; |
| 17 | 17 | |
| 18 | -(* The bits of x, and the F64 with bits u. *) | |
| 18 | +(* The bits of x, and the F64 with bits u: a view of each, its bytes | |
| 19 | + read as the other type (report section 6.9). *) | |
| 19 | 20 | proc bits*(x: F64): U64; |
| 20 | -var p: ^U64; | |
| 21 | 21 | begin |
| 22 | - p := ^U64(ADR(x)); | |
| 23 | - return p^; | |
| 22 | + return x.(U64); | |
| 24 | 23 | end; |
| 25 | 24 | |
| 26 | 25 | proc fromBits*(u: U64): F64; |
| 27 | -var p: ^F64; | |
| 28 | 26 | begin |
| 29 | - p := ^F64(ADR(u)); | |
| 30 | - return p^; | |
| 27 | + return u.(F64); | |
| 31 | 28 | end; |
| 32 | 29 | |
| 33 | 30 | (* Whether x has its sign bit set, -0.0 included. *) |
| @@ -39,7 +39,7 @@ | ||
| 39 | 39 | |
| 40 | 40 | Lengths, indexes, counts and sizes are `INT`s. `LEN(s)` of a slice is an `INT`, and so is the loop variable that walks it. It is as big as an address, so it is the integer a pointer converts to, and the word the atomics work on. Reach for `INT` unless you have a reason: a byte is a `U8`, a character an `I32`, a file's size an `I64`, a hash a `U64`. |
| 41 | 41 | |
| 42 | -`INT` and `I64` are two types even where they have the same size. An `INT` goes silently into an `I64` (and a `UINT` into a `U64`), which is why `Out.int(n)` and `Fmt.int(n)` take an `I64` and accept any `INT`. Nothing else converts unasked: any other integer is converted where it is written, as `Out.int(I64(b))`. | |
| 42 | +`INT` and `I64` are two types even where they have the same size. An `INT` goes silently into an `I64` (and a `UINT` into a `U64`) wherever it meets one, in `pos + LEN(s)` as in a call, which is why `Out.int(n)` and `Fmt.int(n)` take an `I64` and accept any `INT`. The other way is written, `INT(pos)`, since an `INT` may be smaller on another machine. Nothing else converts unasked: any other integer is converted where it is written, as `Out.int(I64(b))`. | |
| 43 | 43 | |
| 44 | 44 | ## A number written in the program |
| 45 | 45 |
| @@ -9,7 +9,7 @@ | ||
| 9 | 9 | |
| 10 | 10 | ## Conversions are written |
| 11 | 11 | |
| 12 | -Two types never mix: with `i: I64` and a slice `s`, `i < LEN(s)` is an error. A type's name converts: `F64(n)`, `I64(b)`, `U8(x)`. The one silent conversion between numbers is an `INT` into an `I64`, which loses nothing. The [lesson on integers](integers.html) says what each width conversion does. | |
| 12 | +Two types never mix but for one: an `INT` goes into an `I64` wherever it meets one, which loses nothing, so with `pos: I64`, `pos + LEN(s)` is an `I64`. With `i: I32` and a slice `s`, `i < LEN(s)` is an error. A type's name converts: `F64(n)`, `I64(b)`, `U8(x)`; an `I64` becomes an `INT` only so, `INT(pos)`, since an `INT` may be smaller on another machine. The [lesson on integers](integers.html) says what each width conversion does. | |
| 13 | 13 | |
| 14 | 14 | A number written in the program is untyped until it meets a type, and `1.5` is a real, which an integer never becomes without a conversion. |
| 15 | 15 | |
| @@ -18,7 +18,7 @@ | ||
| 18 | 18 | import Out; |
| 19 | 19 | |
| 20 | 20 | proc main*; |
| 21 | -var i: I64; s: []U8; | |
| 21 | +var i: I32; s: []U8; | |
| 22 | 22 | begin |
| 23 | 23 | s := "four"; |
| 24 | 24 | i := 3; |
| @@ -32,7 +32,7 @@ | ||
| 32 | 32 | $ raz Mix.raz |
| 33 | 33 | ``` |
| 34 | 34 | |
| 35 | -The fix is the conversion, written where it happens: `if i < I64(LEN(s))`. It costs nothing at run time and says what is meant. | |
| 35 | +The fix is the conversion, written where it happens: `if INT(i) < LEN(s)`. It costs nothing at run time and says what is meant. | |
| 36 | 36 | |
| 37 | 37 | ## What converts to what |
| 38 | 38 |
| @@ -97,6 +97,69 @@ | ||
| 97 | 97 | |
| 98 | 98 | A field of a packed record may sit at any address, and is read and written like any other; but it has no address of its own to take with `ADR`. |
| 99 | 99 | |
| 100 | +## Bytes as numbers and records: views | |
| 101 | + | |
| 102 | +A file, a socket or a device hands a program bytes. A view reads and writes them as another type: `msg[3].(U32)` is the `U32` whose four bytes start at `msg[3]`, and `msg[0].(Head)` the `Head` whose bytes start at `msg[0]`. | |
| 103 | + | |
| 104 | +```raz Wire run | |
| 105 | +module Wire; | |
| 106 | +import Out; | |
| 107 | + | |
| 108 | +type Head = packed record magic: U16; kind: U8; len: U32; end; (* 7 bytes, as sent *) | |
| 109 | + | |
| 110 | +var msg: [32]U8; | |
| 111 | + | |
| 112 | +proc main*; | |
| 113 | +var h: Head; i: INT; sum: I64; | |
| 114 | +begin | |
| 115 | + (* what a socket might have handed us *) | |
| 116 | + msg[0] := 52H; msg[1] := 5AH; | |
| 117 | + msg[2] := 1; | |
| 118 | + msg[3].(U32) := 8; | |
| 119 | + msg[7].(I32) := 100; | |
| 120 | + msg[11].(I32) := 200; | |
| 121 | + | |
| 122 | + h := msg[0].(Head); | |
| 123 | + Out.hex(U64(h.magic), 4); Out.char(' '); | |
| 124 | + Out.int(I64(h.kind)); Out.char(' '); | |
| 125 | + Out.int(I64(h.len)); Out.ln; | |
| 126 | + sum := 0; | |
| 127 | + i := 7; | |
| 128 | + while i < 7 + INT(h.len) do | |
| 129 | + sum := sum + I64(msg[i].(I32)); | |
| 130 | + i := i + 4; | |
| 131 | + end; | |
| 132 | + Out.int(sum); Out.ln; | |
| 133 | +end; | |
| 134 | + | |
| 135 | +end Wire. | |
| 136 | +``` | |
| 137 | + | |
| 138 | +A view isn't a copy: it *is* those bytes, so `msg[3].(U32) := 8` writes `msg[3]` to `msg[6]`, and a view goes wherever a variable does, with a field after it, in `INC`, on the left of `:=`. Nor is it a conversion: `I64(x)` is a real's number, `x.(U64)` its bits. The magic number reads `5A52`, its bytes backwards, because x86-64 keeps a number's low byte first; a format with an order of its own is read with `Str.le` or `Str.be`. | |
| 139 | + | |
| 140 | +A view of an element must end inside the array, and the program checks it as it checks an index: | |
| 141 | + | |
| 142 | +```raz Short fail | |
| 143 | +module Short; | |
| 144 | +import Out; | |
| 145 | + | |
| 146 | +var msg: [10]U8; | |
| 147 | + | |
| 148 | +proc main*; | |
| 149 | +var i: INT; | |
| 150 | +begin | |
| 151 | + i := 0; | |
| 152 | + while i < LEN(msg) do | |
| 153 | + Out.int(I64(msg[i].(U32))); Out.char(' '); | |
| 154 | + i := i + 4; | |
| 155 | + end; | |
| 156 | +end; | |
| 157 | + | |
| 158 | +end Short. | |
| 159 | +``` | |
| 160 | + | |
| 161 | +The type of a view is an integer, a real or a pointer, or a record or array made of them: types any bytes are a value of, which a `BOOL` or an enumeration isn't. A view may start at any byte, so, as a packed record's field, it has no address of its own. | |
| 162 | + | |
| 100 | 163 | ## Pointers |
| 101 | 164 | |
| 102 | 165 | `ADR(x)` takes the address of a variable, `^T` is a pointer to a `T`, and `p^` is what it points at. A pointer that points nowhere is `nil`, and following it stops the program with a message that names the place, so a forgotten check is found at once instead of corrupting memory. |
| @@ -214,6 +214,12 @@ | ||
| 214 | 214 | var text: []U8; |
| 215 | 215 | begin |
| 216 | 216 | text := say2("{} and {} are different types", Sym.typeName(xt), Sym.typeName(yt)); |
| 217 | + (* the help converts the narrower integer to the wider, which loses | |
| 218 | + nothing; else the right operand to the left one's type *) | |
| 219 | + if Sym.isInt(xt) and Sym.isInt(yt) and (xt^.size < yt^.size) and (xt # Sym.tConst) then | |
| 220 | + return Scan.markHelp(Diag.join(x.pos, y.pos), text, | |
| 221 | + say2("convert one to the other's type, as in {}({})", Sym.typeName(yt), Diag.source(x.pos))); | |
| 222 | + end; | |
| 217 | 223 | if (Sym.isInt(xt) or (xt^.form = Sym.fReal)) and (Sym.isInt(yt) or (yt^.form = Sym.fReal)) |
| 218 | 224 | and (xt # Sym.tConst) and (xt # Sym.tRConst) then |
| 219 | 225 | return Scan.markHelp(Diag.join(x.pos, y.pos), text, |
| @@ -9,14 +9,17 @@ | ||
| 9 | 9 | # 2026-10.25): the files that use them are the compiler's alone, and |
| 10 | 10 | # so are those whose message says what match takes. It predates, too, |
| 11 | 11 | # an INT widened in an expression and the rest (report 2026-10.26), |
| 12 | -# and the messages for an I64 that goes to an INT; and views, v.(T), | |
| 13 | -# whose tests are conform/*/View*.raz. | |
| 12 | +# the messages for an I64 that goes to an INT, and a help that | |
| 13 | +# converts the narrower of two integers; and views, v.(T), | |
| 14 | +# whose tests are conform/*/View*.raz, and which Math, examples/Elf.raz | |
| 15 | +# and, through Math, test/bench/NBody.raz use. | |
| 14 | 16 | newer="conform/run/RawStrings.raz conform/err/ScanRawOpen.raz conform/err/ScanRawIndent.raz |
| 15 | 17 | conform/run/MatchStr.raz conform/err/MatchStrTwice.raz conform/err/MatchStrNoElse.raz |
| 16 | 18 | conform/err/MatchStrVar.raz conform/err/MatchReal.raz conform/err/AtMatch.raz |
| 17 | 19 | examples/Sh.raz examples/Settings.raz |
| 18 | 20 | conform/run/IntWiden.raz conform/err/IntIncNarrow.raz conform/err/IntNarrow.raz conform/err/IntNarrowSum.raz |
| 19 | - conform/err/IntResultsNarrow.raz" | |
| 21 | + conform/err/IntResultsNarrow.raz conform/err/GenIntTypes.raz | |
| 22 | + lib/Math.raz examples/Elf.raz test/bench/NBody.raz" | |
| 20 | 23 | newer() { |
| 21 | 24 | case $1 in conform/*/View*.raz) return 0 ;; esac |
| 22 | 25 | for g in $newer; do [ $g = $1 ] && return 0; done |
| @@ -0,0 +1 @@ | ||
| 1 | +2 |
| @@ -1,5 +1,5 @@ | ||
| 1 | 1 | conform/err/GenIntTypes.raz:6:10: error: I32 and I64 are different types |
| 2 | 2 | 6 | b := a + b; |
| 3 | 3 | | ^^^^^ |
| 4 | -help: convert one to the other's type, as in I32(b) | |
| 4 | +help: convert one to the other's type, as in I64(a) | |
| 5 | 5 | raz: 1 error |