| | @@ -3382,13 +3382,33 @@ |
| 3382 | 3382 | return n; |
| 3383 | 3383 | end; |
| 3384 | 3384 | |
| 3385 | +(* A constant typed []T, or an array literal for a slice part of one, |
| 3386 | + is an array constant whose length is its literal's (report, 4): t, |
| 3387 | + the slice type, becomes [n]T, as if n had been written, with n |
| 3388 | + counted ahead (literalLength). With no element, [], it stays []T: |
| 3389 | + the empty slice, kept as 16 zero bytes. *) |
| 3390 | +proc literalArray(t: PType): (PType, ERROR); |
| 3391 | +var a: PType; n: INT; |
| 3392 | +begin |
| 3393 | + if Scan.tok # Scan.sLbrak then |
| 3394 | + return nil, Scan.markAt(Scan.tokPos(), say1("expected a literal of {}", Sym.typeName(t))); |
| 3395 | + end; |
| 3396 | + n := literalLength(); |
| 3397 | + if n = 0 then return t, nil; end; |
| 3398 | + a := Sym.newType(Sym.fArray, I64(n) * t^.base^.size, t^.base^.align); |
| 3399 | + a^.len := I64(n); |
| 3400 | + a^.base := t^.base; |
| 3401 | + return a, nil; |
| 3402 | +end; |
| 3403 | + |
| 3404 | +proc constPart(t: PType; off: INT): ERROR; forward; |
| 3405 | +proc constChunk(t: PType; n: INT): (INT, ERROR); forward; |
| 3406 | + |
| 3385 | 3407 | (* A variant's case built from constants, at offset off of the constant |
| 3386 | 3408 | (report section 4): its number, then each field where the case lays |
| 3387 | 3409 | it out, as constructCase builds one in a program; the rest stays |
| 3388 | 3410 | zero. The case is read here, by its name, since expression would |
| 3389 | 3411 | make the code that builds one. *) |
| 3390 | | -proc constPart(t: PType; off: INT): ERROR; forward; |
| 3391 | | - |
| 3392 | 3412 | proc constCase(t: PType; off: INT): ERROR; |
| 3393 | 3413 | var c, f: PObj; at: Diag.Pos; |
| 3394 | 3414 | begin |
| | @@ -3415,13 +3435,27 @@ |
| 3415 | 3435 | |
| 3416 | 3436 | (* The part of type t at offset off of the constant *) |
| 3417 | 3437 | proc constPart(t: PType; off: INT): ERROR; |
| 3418 | | -var e: TItem; f: PObj; k: INT; r: F32; start: Diag.Pos; src: ^U8; st, fx: INT; |
| 3438 | +var e: TItem; f: PObj; k, c: INT; r: F32; start: Diag.Pos; src: ^U8; st, fx: INT; a: PType; |
| 3419 | 3439 | begin |
| 3420 | 3440 | start := Scan.tokPos(); |
| 3421 | | - if (t^.form = Sym.fSlice) and (Scan.tok = Scan.sLbrak) and (literalLength() = 0) then |
| 3422 | | - (* [], the empty slice (report, 4): its bytes stay zero, ptr nil and len 0 *) |
| 3423 | | - Scan.next; |
| 3424 | | - return expect(Scan.sRbrak); |
| 3441 | + if (t^.form = Sym.fSlice) and (Scan.tok = Scan.sLbrak) then |
| 3442 | + a := try literalArray(t); |
| 3443 | + if a = t then |
| 3444 | + (* [], the empty slice (report, 4): its bytes stay zero, ptr nil and len 0 *) |
| 3445 | + Scan.next; |
| 3446 | + return expect(Scan.sRbrak); |
| 3447 | + end; |
| 3448 | + (* an array literal (report, 4) is an array constant of its own, |
| 3449 | + in a chunk of its own, made before this one is; the slice |
| 3450 | + points at it, as it would at a named one: |
| 3451 | + const Rows: [][]INT = [[1, 2], [3]]; |
| 3452 | + is laid out as |
| 3453 | + const R0: [2]INT = [1, 2]; R1: [1]INT = [3]; |
| 3454 | + const Rows: [][]INT = [R0, R1]; *) |
| 3455 | + c := try constChunk(a, -1); |
| 3456 | + try constFix(off, c); |
| 3457 | + putBytes(off + 8, 8, a^.len); |
| 3458 | + return nil; |
| 3425 | 3459 | end; |
| 3426 | 3460 | if (t^.form = Sym.fArray) and (Scan.tok = Scan.sLbrak) then |
| 3427 | 3461 | Scan.next; |
| | @@ -3460,6 +3494,9 @@ |
| 3460 | 3494 | return expect(Scan.sRbrace); |
| 3461 | 3495 | end; |
| 3462 | 3496 | if (t^.form = Sym.fVariant) and (Scan.tok = Scan.sIdent) then return constCase(t, off); end; |
| 3497 | + if Scan.tok = Scan.sLbrak then (* an array literal where neither an array nor a slice is *) |
| 3498 | + return Scan.mark(say1("expected {}, found an array literal", Sym.typeName(t))); |
| 3499 | + end; |
| 3463 | 3500 | try expression(e, t); |
| 3464 | 3501 | if (e.mode = Item.mStr) and ((t^.form = Sym.fSlice) or (t^.form = Sym.fArray)) and (t^.base = Sym.tU8) then |
| 3465 | 3502 | if t^.form = Sym.fSlice then |
| | @@ -3476,7 +3513,7 @@ |
| 3476 | 3513 | it, which has a type of its own: the element's, or a slice's *) |
| 3477 | 3514 | if (e.mode # Item.mVar) or (e.obj = nil) or (e.obj^.cls # Sym.oConst) or (e.typ # e.obj^.typ) |
| 3478 | 3515 | or not (e.typ^.form in {Sym.fArray, Sym.fSlice}) or not Sym.same(e.typ^.base, t^.base) then |
| 3479 | | - return Scan.markAt(e.pos, say1("a constant slice is a string, an array constant of its elements or [], found {}", Item.what(e))); |
| 3516 | + return Scan.markAt(e.pos, say1("a constant slice is a string, an array constant or literal of its elements, or [], found {}", Item.what(e))); |
| 3480 | 3517 | end; |
| 3481 | 3518 | if e.typ^.form = Sym.fArray then (* else an empty slice constant, whose bytes stay zero *) |
| 3482 | 3519 | try constFix(off, e.chunk); |
| | @@ -3517,37 +3554,37 @@ |
| 3517 | 3554 | return nil; |
| 3518 | 3555 | end; |
| 3519 | 3556 | |
| 3520 | | -(* The chunk of n, a constant of type t whose literal is next *) |
| 3521 | | -proc aggregateConst(t: PType; n: INT): (INT, ERROR); |
| 3522 | | -var c, i, size: INT; |
| 3557 | +(* The chunk of n, a constant of type t whose literal is next, or of |
| 3558 | + an array literal in a constant's slice part, with n -1. That one is |
| 3559 | + made while the constant around it is still being read: its bytes are |
| 3560 | + its own, and the constant's are put back after; its addresses go on |
| 3561 | + in fixes after the constant's, from base, and are taken off again. *) |
| 3562 | +proc constChunk(t: PType; n: INT): (INT, ERROR); |
| 3563 | +var c, i, size, base: INT; outer: ^U8; |
| 3523 | 3564 | begin |
| 3565 | + outer := constBytes; |
| 3566 | + base := numFixes; |
| 3524 | 3567 | size := INT(t^.size); |
| 3525 | 3568 | constBytes := Scan.grab(size + 8); |
| 3526 | 3569 | for i := 0 to size + 7 do constBytes[i] := 0; end; |
| 3527 | | - numFixes := 0; |
| 3528 | 3570 | try constPart(t, 0); |
| 3529 | | - c := Chunk.newChunk(Chunk.secRodata, Sym.curModule^.sname, n); |
| 3571 | + if n < 0 then |
| 3572 | + c := Chunk.newChunk(Chunk.secRodata, -1, -1); |
| 3573 | + else |
| 3574 | + c := Chunk.newChunk(Chunk.secRodata, Sym.curModule^.sname, n); |
| 3575 | + end; |
| 3530 | 3576 | Chunk.setData(c, constBytes, size, INT(t^.align)); |
| 3531 | | - for i := 0 to numFixes - 1 do Chunk.addDataFixup(c, fixes[i].off, fixes[i].chunk, 0); end; |
| 3577 | + for i := base to numFixes - 1 do Chunk.addDataFixup(c, fixes[i].off, fixes[i].chunk, 0); end; |
| 3578 | + numFixes := base; |
| 3579 | + constBytes := outer; |
| 3532 | 3580 | return c, nil; |
| 3533 | 3581 | end; |
| 3534 | 3582 | |
| 3535 | | -(* A constant typed []T is an array constant whose length is its |
| 3536 | | - literal's (report, 4): t, the slice type, becomes [n]T, as if n had |
| 3537 | | - been written, with n counted ahead (literalLength). With no element, |
| 3538 | | - [], it stays []T: the empty slice, kept as 16 zero bytes. *) |
| 3539 | | -proc literalArray(t: PType): (PType, ERROR); |
| 3540 | | -var a: PType; n: INT; |
| 3583 | +(* The chunk of n, a constant of type t whose literal is next *) |
| 3584 | +proc aggregateConst(t: PType; n: INT): (INT, ERROR); |
| 3541 | 3585 | begin |
| 3542 | | - if Scan.tok # Scan.sLbrak then |
| 3543 | | - return nil, Scan.markAt(Scan.tokPos(), say1("expected a literal of {}", Sym.typeName(t))); |
| 3544 | | - end; |
| 3545 | | - n := literalLength(); |
| 3546 | | - if n = 0 then return t, nil; end; |
| 3547 | | - a := Sym.newType(Sym.fArray, I64(n) * t^.base^.size, t^.base^.align); |
| 3548 | | - a^.len := I64(n); |
| 3549 | | - a^.base := t^.base; |
| 3550 | | - return a, nil; |
| 3586 | + numFixes := 0; (* none left over from a constant an error broke off *) |
| 3587 | + return constChunk(t, n); |
| 3551 | 3588 | end; |
| 3552 | 3589 | |
| 3553 | 3590 | proc constDecl(top: BOOL): ERROR; |