Author Topic: Unleashed Pascal (async/await, parallel for, match, string interpolation & more)  (Read 69936 times)

440bx

  • Hero Member
  • *****
  • Posts: 6627
The fix for block-scoped inline variables is basically done, but...
I consider that a significant improvement... thank you for taking care of that problem.

How about I push the commits as-is (with proper DW_TAG_lexical_block), and someone familiar with FpDebug picks up the fix? The core issue is that FpDebug does not descend into DW_TAG_lexical_block when enumerating local variables.
I would favor that and consider it the correct course of action/solution.  Let's see what the Lazarus team decides to do.

FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12818
  • Debugger - SynEdit - and more
    • wiki
So far FpDebug had (mostly) only testing against fpc generated data.

I did have a very little time to test with fpc llvm backend, and even less with gcc (and still have some todo with arrays there).
That also means that the feature set currently has concentrated on the parts emitted by FPC.

The only "on top addition" so far is location lists. (comes with the llvm backend, when optimized code uses registers)

However, that is not a design decision, but just what fitted into time, and what is prioritised. (obviously debugging fpc generated code is the highest priority).

The final aim however is a full DWARF debugger working with input from any compiler. So based on that, its acceptable and desirable to add.




Have to think about the "how to add" (even in the light of discussing a patch).

In the example (dwarf 5 spec) the DW_TAG_lexical_block  is nested into a subroutine.
That would make it easy to find (i.e. it can be accessed via the already know "current subroutine"). It would not need additional lookups (outside that scope) ....

However, the actual description of it, does not specify that.... I wonder if there may be such blocks outside subroutines  (potentially overlapping???). Not that I can see much sense in that, but ...



Also even inside a subroutine, there is thought about resolving order.

If the same name (for a variable) is in the subroutine and in the DW_TAG_lexical_block => the first found shall be used (I guess). Showing both will seriously complicate matters.
But in which order should that search happen.

And if inside the subroutine there are several overlapping DW_TAG_lexical_block ?



As a first thought I would think.

- treat them as nested in subroutines (or other parent, but we don't eg have callsites at current).
- if found outside, find all subroutines by overlapping addresses, and create links
=> so then they can always be found from the subroutine

- search them before the outer subroutine (in line with all else, start in the inner nested definition)
- search them (sibling with address overlap) in the order they appear (at least if they are declared as child of the subroutine / if they are outside: not sure)





hint on fpdebug

there are classes for DWARF types and data symbols => those should (not currently always obeyed) be an immutable representation of the dwarf info.

There are for each of them value classes that are used to compute results...

I have to lookup the naming schemes...



If you whish to explore this deeper, maybe start a new thread.

Fibonacci

  • Hero Member
  • *****
  • Posts: 1123
  • Behold, I bring salvation - Unleashed Pascal
    • fibo.gg
Also even inside a subroutine, there is thought about resolving order.

If the same name (for a variable) is in the subroutine and in the DW_TAG_lexical_block => the first found shall be used (I guess). Showing both will seriously complicate matters.
But in which order should that search happen.

And if inside the subroutine there are several overlapping DW_TAG_lexical_block ?

Code: Pascal  [Select][+][-]
  1. procedure main;
  2. var
  3.   i: integer;
  4. begin
  5.   begin
  6.     var i := 1; // not allowed: Error: Duplicate identifier "i"
  7.   end;
  8. end;

Code: Pascal  [Select][+][-]
  1. procedure main;
  2. begin
  3.   begin
  4.     var i := 1;
  5.  
  6.     begin
  7.       var i := 2; // not allowed: Error: Duplicate identifier "i"
  8.     end;
  9.   end;
  10. end;



I'll push the commits soon, just need to clean up the mess and fix a bug I found along the way.
Unleashed Pascal: async/await, parallel for, match, tuples, string interpolation, inline vars, autofree, no-RTTI & tons more. Star on GitHub ⭐

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12818
  • Debugger - SynEdit - and more
    • wiki
There isn't just the use of DW_TAG_lexical_block  by your implementation.

Other compilers will emit it too. If implemented it should work for them too (or at least not block adding them later).

Fibonacci

  • Hero Member
  • *****
  • Posts: 1123
  • Behold, I bring salvation - Unleashed Pascal
    • fibo.gg
I committed the inline variable changes to the "devel" branch on GitHub. Just FPC.

The inline variables are now block-scoped (Delphi-style). If someone could test the devel branch, that would be great. Inline variables should behave like in Delphi - they are scoped to their declaring begin..end block, and shadowing a variable from an enclosing block scope is not allowed.

Local Variables in the debugger should work correctly with GDB. FpDebug support is a separate effort - I don't think it's a lot of work, but I might be wrong.

Waiting for feedback before moving forward.
« Last Edit: April 04, 2026, 10:50:10 pm by Fibonacci »
Unleashed Pascal: async/await, parallel for, match, tuples, string interpolation, inline vars, autofree, no-RTTI & tons more. Star on GitHub ⭐

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12818
  • Debugger - SynEdit - and more
    • wiki
Start at
function TFpDwarfInfoSymbolScope.FindLocalSymbol(const AName: String;
that searches the direct members of the subroutine.

There is an inherited method, that takes care of nested procedures and $parentpf => that also may need changes, not sure...

-----

Don't just add to GoNamedChildEx. That is called from to many places.

Maybe add
TFpDwarfInfoSymbolScope.FindLexicalBlock => and inside that then do GoNamedChildEx.




function TFpDwarfInfoSymbolScope.FindSymbol
should be a good start to see how to walk the dwarf info ....




I do not know about your usage. But I read they can be nested.

So if FindLexicalBlock finds a block it must
- first do a nested FindLexicalBlock
- then go to namedChildEx


« Last Edit: April 04, 2026, 10:04:57 pm by Martin_fr »

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12818
  • Debugger - SynEdit - and more
    • wiki
Testcase  (in the lazdebugger/lazdebuggerFP dir) / not sure if there is a wiki page
=== if you don't get it to run, I will anyway run it myself (for official FPC). So not a must, just in case it interests you


Look at the file fpclist.txt => it needs at least one valid fpc compiler.
Well, if you test, then likely at least 2
- yours
- an official FPC (so your changes can be tested there)

On Linux, ensure your RTL (if it has debug info) matches the rest of debug info. Any mix between dwarf versions will give errors (kind of false positives ...)

create dirs
- logs
- appdata/lib
- on Linux you may need a sym link to appdata as AppData

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12818
  • Debugger - SynEdit - and more
    • wiki
Ok, so function TFpDwarfInfoSymbolScope.FindSymbol will allow to evaluate the variables (add them to watches).
It will also be needed for locals, since they also need to eval the value...

The list is gotten from the members of the subroutine
procedure TFpThreadWorkerLocals.DoExecute;

But the member property really should do what it currently does. So this needs a new function to get current locals.

----------------
I do have to give that a 2nd thought, if this is the correct approach....

You have   
   LocalScope: TFpDbgSymbolScope;

which IIRC is a TFpDwarfInfoSymbolScope.

So you can add methods/properties to the scope (virtual, empty/overriden with content

LocalsCount
Locals[n]: TFpValue

That checks if FSymbol is a TFpSymbolDwarfDataProc (like in FindSymbol).
Then it can deliver all that is needed.




Getting the count upfront is a bit akward.

Currently all code that needs stuff like this, compiles internally a list, so it can later return members from that list....
Probably going that way for now (so add one list field FLocals to the scope).

There are better ways, but then it becomes a much bigger change, and much harder to review and add.


« Last Edit: April 04, 2026, 10:27:11 pm by Martin_fr »

Fibonacci

  • Hero Member
  • *****
  • Posts: 1123
  • Behold, I bring salvation - Unleashed Pascal
    • fibo.gg
Proposal: Tuples

Meanwhile, while we are waiting for any feedback :-) or for my tests to show any troubles with recent features/fixes, how do you feel about...

Tuples in FPC?

I have been thinking about this for a while and I believe tuples would be a natural next step for FPC Unleashed. The core idea is simple: allow functions to return multiple values without forcing the programmer to declare a named record type every single time. Under the hood, a tuple is just an anonymous record - so we reuse the existing record infrastructure entirely. No new runtime concepts, no new memory layouts, just syntactic convenience backed by something the compiler already understands.

Here is what I have in mind.

1. Positional tuples as return types

The simplest form. You list the types, the compiler generates an anonymous record with fields named _1, _2, _3, etc.

Code: Pascal  [Select][+][-]
  1. function divmod(a, b: Integer): (Integer, Integer);
  2. begin
  3.   result._1 := a div b;
  4.   result._2 := a mod b;
  5. end;
  6.  

2. Named tuples as return types

Same thing, but you give the fields proper names. The syntax mirrors record field declarations.

Code: Pascal  [Select][+][-]
  1. function get_coords: (x, y: Integer);
  2. begin
  3.   result.x := 10;
  4.   result.y := 20;
  5. end;
  6.  

3. Tuple literals

Instead of assigning fields one by one, you can assign the whole tuple at once. For positional tuples:

Code: Pascal  [Select][+][-]
  1. function divmod(a, b: Integer): (Integer, Integer);
  2. begin
  3.   result := (a div b, a mod b);
  4. end;
  5.  

For named tuples, the delimiter is a comma (not semicolon - this is not a record declaration, it is a value expression). You can also reorder fields freely:

Code: Pascal  [Select][+][-]
  1. function get_coords: (x, y: Integer);
  2. begin
  3.   result := (y: 20, x: 10);  // any order
  4. end;
  5.  

Disambiguation with parenthesized expressions: a tuple literal must have 2 or more elements. (42) remains a plain arithmetic expression - no 1-element tuples.

4. Exit() with multiple values

When a function returns a tuple and Exit() receives more than one argument, it wraps them into a tuple automatically. No extra parentheses needed:

Code: Pascal  [Select][+][-]
  1. function foo: (Integer, Integer);
  2. begin
  3.   if condition then
  4.     Exit(0, 0);  // not Exit((0, 0))
  5.   result := (100, 200);
  6. end;
  7.  
  8. function bar: (a, b: Integer);
  9. begin
  10.   Exit(a: 1, b: 2);  // named exit
  11. end;
  12.  

5. Array of tuple

Tuples work as array element types, including with array literals:

Code: Pascal  [Select][+][-]
  1. var
  2.   points: array of (x, y: Integer);
  3.   pairs: array of (Integer, String);
  4. begin
  5.   points := [(1, 2), (3, 4), (5, 6)];
  6. end;
  7.  

6. Generics

Generic type parameters can appear inside tuples. Specialization produces a concrete anonymous record as usual:

Code: Pascal  [Select][+][-]
  1. function Zip<A, B>(xs: array of A; ys: array of B): array of (A, B);
  2.  

7. Destructuring (already works in FPC Unleashed)

FPC Unleashed already supports inline variable declarations with destructuring. Tuple returns would naturally work with this:

Code: Pascal  [Select][+][-]
  1. var (q, r) := divmod(17, 5);
  2. // q = 3, r = 2
  3.  

What we are NOT doing (at least not in the first version):

  • No tuples as parameter types. If you need multiple arguments, just declare multiple arguments - wrapping them in a tuple adds nothing.
  • No structural compatibility between tuples and named records. This could be added later,
  • No 1-element tuples. (42) is a parenthesized expression, period.
  • No structural equality operator for tuples (yet).

Implementation strategy

The key principle: a tuple is an anonymous record. Nothing more.

(x, y: Integer) becomes an internal record with fields x and y of type Integer. (Integer, String) becomes an internal record with fields _1: Integer and _2: String.

This means:
  • result.field access works automatically - it is a record.
  • Per-field assignment works automatically.
  • Managed types, RTTI, copy semantics - all free from the record infrastructure.
  • Passing by value/const/var - already works for records.

The actual work is mostly in the parser: recognizing the tuple syntax in return type positions, generating the anonymous trecorddef with the right fields, and handling tuple literals as syntactic sugar for per-field assignment. The heaviest lifting is already done by the existing record machinery.

Thoughts? Objections? Things I am missing? A "no no", not Pascal-enough?
Unleashed Pascal: async/await, parallel for, match, tuples, string interpolation, inline vars, autofree, no-RTTI & tons more. Star on GitHub ⭐

440bx

  • Hero Member
  • *****
  • Posts: 6627
Thoughts? Objections? Things I am missing? A "no no", not Pascal-enough?
Not really an objection but, your proposal really seems to be pure syntactic sugar which doesn't seem to add anything truly beneficial to the language. 

One case I would not be in favor of is the "positional tuples as return types" as this forces the compiler to dynamically generate names (apparently counted, _1, _2) which means the programmer needs to be aware of compiler internals, i.e, the names the compiler will generate and in what order.  In this particular case it's not hard to figure out but, it is not something the programmer should know.

Another thing I'm not fond of is that, while it adds no capabilities to the language, IMO, it does require more changes in the parser than pure syntactic sugar deserves.  A side effect of that is, it requires a fair number of test cases to ensure the implementation is correct.

Personally, my biggest problem with it is, I can do the same with records and I think it is actually cleaner with records.

On a completely different note and, pure curiosity on my part, are you using A.I to implement the features you're adding ? (whatever your answer is, I won't elaborate in this thread.  I may in the Pascal A.I section... depends on your reply, if any, to the question.)
« Last Edit: April 05, 2026, 10:39:37 pm by 440bx »
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

Fibonacci

  • Hero Member
  • *****
  • Posts: 1123
  • Behold, I bring salvation - Unleashed Pascal
    • fibo.gg
One case I would not be in favor of is the "positional tuples as return types" as this forces the compiler to dynamically generate names (apparently counted, _1, _2) which means the programmer needs to be aware of compiler internals, i.e, the names the compiler will generate and in what order.  In this particular case it's not hard to figure out but, it is not something the programmer should know.

In this case the programmer did not name the fields, so they are accessible via pseudo-names _1, _2 and so on. I am also considering using array-style indexing, e.g.:

Code: Pascal  [Select][+][-]
  1. function divmod(a, b: Integer): (Integer, Integer);
  2. begin
  3.   result._1 := a div b;
  4.   result._2 := a mod b;
  5. end;

->

Code: Pascal  [Select][+][-]
  1. function divmod(a, b: Integer): (Integer, Integer);
  2. begin
  3.   result[0] := a div b;
  4.   result[1] := a mod b;
  5. end;

-> Or, just:

Code: Pascal  [Select][+][-]
  1. function divmod(a, b: Integer): (Integer, Integer);
  2. begin
  3.   result := (a div b, a mod b);
  4. end;

On a completely different note and, pure curiosity on my part, are you using A.I to implement the features you're adding ? (whatever your answer is, I won't elaborate in this thread.  I may in the Pascal A.I section... depends on your reply, if any, to the question.)

Of course I do. Not using AI for coding at this point is actively hurting yourself. I have projects that are 10+ years old, running 24/7 to this day - recently AI wrote me a complete drop-in replacement unit for one of them. Works great, and it would have taken me weeks to write manually.

AI tends to make ridiculous errors sometimes though. For example, it was not able to write a proper EncodeBase64() - the last-byte encoding to be precise. It spent an hour changing things and going deeper and deeper into the source trying to fix it, while I just said "stop". Debugged it myself, fixed it manually, verified, and then continued the AI task. Someone who does not know the programming language would have been stuck there forever. AI without a programmer who understands the code is useless. But a programmer with AI - that is a huge boost, like multiplying what you already know by 50.

That said, I started FPC Unleashed well before I started using AI. It is only recently that I began incorporating it into my workflow.
Unleashed Pascal: async/await, parallel for, match, tuples, string interpolation, inline vars, autofree, no-RTTI & tons more. Star on GitHub ⭐

440bx

  • Hero Member
  • *****
  • Posts: 6627
Fibonacci, thank you for the information about A.I. 

Of the three methods you suggested, syntactically I like the one that uses indexes best.  That said, it concerns me that it is using indexing for something that is not an array.  That has the potential to eventually create ambiguities or be confusing if the function internally uses arrays.

The one I like the least is the third option because it requires both result values to be set simultaneously which may not always be possible, e.g, when one of the values is somehow dependent on the other, in that case having to set them both at the same time may be problematic.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

Fibonacci

  • Hero Member
  • *****
  • Posts: 1123
  • Behold, I bring salvation - Unleashed Pascal
    • fibo.gg
There is a choice between 1 and 2 - or both at once, no reason they cannot coexist. But 3 exists regardless - you need tuple literals anyway, if only for Exit(1, 2) or result := (10, 20). As for setting both values simultaneously being problematic when one depends on the other - you can always fall back to per-field assignment in that case. The literal form is a convenience, not a requirement.
Unleashed Pascal: async/await, parallel for, match, tuples, string interpolation, inline vars, autofree, no-RTTI & tons more. Star on GitHub ⭐

440bx

  • Hero Member
  • *****
  • Posts: 6627
If I understood you correctly, all three forms would be simultaneously available, is this correct ?
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

Fibonacci

  • Hero Member
  • *****
  • Posts: 1123
  • Behold, I bring salvation - Unleashed Pascal
    • fibo.gg
This one:

Code: Pascal  [Select][+][-]
  1. function divmod(a, b: Integer): (Integer, Integer);
  2. begin
  3.   result := (a div b, a mod b);
  4. end;

And one of these (or both):

Code: Pascal  [Select][+][-]
  1. function divmod(a, b: Integer): (Integer, Integer);
  2. begin
  3.   result._1 := a div b;
  4.   result._2 := a mod b;
  5. end;

Code: Pascal  [Select][+][-]
  1. function divmod(a, b: Integer): (Integer, Integer);
  2. begin
  3.   result[0] := a div b;
  4.   result[1] := a mod b;
  5. end;
Unleashed Pascal: async/await, parallel for, match, tuples, string interpolation, inline vars, autofree, no-RTTI & tons more. Star on GitHub ⭐

 

TinyPortal © 2005-2018