Re: [fpc-pascal] Translate C code
Hi, On Sat, 6 Jan 2018, Darius Blaszyk wrote: > Again I would like to ask some support on a piece C code that I need to > translate to pascal. The code in question is: > > #define MEMNEXT(x) ((MyStruct *)(((char *) x) - ((char *) & (((MyStruct > *)0)->next > > What exactly is going on here? Where does the result point to? It seems > like it starts with the pointer x and subtracts the size of the offset > of the beginning of MyStruct up to the "next" variable. At least that's > what I think is happening. Yes, it looks like it. It simply substracts the offset of the "next" field from the value of X; This is a better solution than hardwiring the offset of next, which could change depending on the target platform's structure padding, or other reasons (and sometimes could be hard to calculate by hand anyway). > Does anyone has an idea how to translate this into working pascal code? Well, not sure why one needs to do this abomination, without knowing the data structure it operates on, but nevertheless, you can do the same in Free Pascal, but you better turn the macro into a function: type PMyStruct = ^TMyStruct; TMyStruct = record some, fields: integer; next: PMyStruct; end; function MEMNEXT(x: Pointer): PMyStruct; begin MEMNEXT:=PMyStruct(PByte(x)-PByte(@(PMyStruct(nil)^.next))); end; If this function is used often, you can mark it as inline; So the compiler can inline it, whenever necessary. If you want to avoid pointermath for whatever reason, the same works if you cast the pointers to PtrUInt instead of a PByte. If the input value of X is other than a pointer, you can either overloaded methods, or define a var parameter without type, for example. Charlie ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
[fpc-pascal] Translate C code
Hi all, Again I would like to ask some support on a piece C code that I need to translate to pascal. The code in question is: #define MEMNEXT(x) ((MyStruct *)(((char *) x) - ((char *) & (((MyStruct *)0)->next What exactly is going on here? Where does the result point to? It seems like it starts with the pointer x and subtracts the size of the offset of the beginning of MyStruct up to the "next" variable. At least that's what I think is happening. Does anyone has an idea how to translate this into working pascal code? TIA! Rgds, Darius___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Modern Compiler Construction using Pascal
On 2018-01-02 15:30, Marc Santhoff wrote: Sorry for mentioning something nasty like Java on this list. The more I use Java (and the ecosystem around it) the more I love it. :) It is expected that any developer these days are proficient in multiple languages. Object Pascal (Delphi) and Java are good combinations/choices in my eyes. Regards, Graeme ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Register Allocation on x86_64
On 05/01/18 16:41, Martok wrote: is it possible that the register allocation on x86_64 is a bit inefficient? No matter the optimization settings, I can never get FPC to use more than the rax and rdx registers. Especially $Optimization REGVAR does nothing (not even for loop variables). Instead, two nested loops are enough to get FPC to constantly do memory load/stores on the loop variables. If an assembler block is marked as using ['rdx', 'rax'] (i.e.: rdtsc), instead of using some other general-purpose registers, FPC stores them to memory before and reloads after the block. That doesn't seem very efficient... regvars have always been disabled for routines that contain assembler blocks (on all architectures). Sometimes the compiler temporarily uses registers for other purposes over a longer period, which is why marking the used registers is still required. Jonas ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
[fpc-pascal] Register Allocation on x86_64
Hi all, is it possible that the register allocation on x86_64 is a bit inefficient? No matter the optimization settings, I can never get FPC to use more than the rax and rdx registers. Especially $Optimization REGVAR does nothing (not even for loop variables). Instead, two nested loops are enough to get FPC to constantly do memory load/stores on the loop variables. If an assembler block is marked as using ['rdx', 'rax'] (i.e.: rdtsc), instead of using some other general-purpose registers, FPC stores them to memory before and reloads after the block. That doesn't seem very efficient... Is there anything else that needs to be switched on? I have the target arch set to -CpCOREAVX2. Regards, Martok ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal