Menu

Variable as array element index issue

ToniG
2021-07-05
2021-07-09
1 2 > >> (Page 1 of 2)
  • ToniG

    ToniG - 2021-07-05

    When using a variable as the array element number, I get zero value bits returned.
    Replace the var A1 with a value & is ok

    ~~~
    Valu(0) = 255
    A1 = 0
    For Index = 0 to 7
    Data1.Index = Valu(A1).Index '< This returns only zero bits
    Data2.Index = Valu(0).Index '< ok
    Next

    ' Workaround...
    Temp = Valu(A1)
    Data1.Index = Temp.Index '< ok
    ~~~

    Please confirm, thanks.
    Files attached...

     

    Last edit: ToniG 2021-07-05
  • Anobium

    Anobium - 2021-07-05

    Agree looks broken with a silent failure. I can resolve to issue a warning that you are using an array element and the compiler cannot resolve for you.

    The core issue is as you have found. You need to take array elements through a temp variable.

    So, I can resolve to issue a warning and therefore not fail silently.

    Yes ?

     
  • ToniG

    ToniG - 2021-07-05

    I suppose if using a variable as the array element number is not allowed when bit indexing (read array element bits).
    but I thought it would be a valid use considering it works the other way around, (write array element bits)...

    Valu(A1).Index = Data1.index
    Valu(0).Index = Data1.index

    Both above lines work, Variable A1 does not cause a dis-function .

     
    • Anobium

      Anobium - 2021-07-05

      Correct that would work and would still work. The destination is ok - it is the source that is the issue.

      Issue warning?

       
  • ToniG

    ToniG - 2021-07-05

    ok

    Is it on a todoo list ? or not able to implement. (I just assumed was just a wee bug as I looked in help)
    I don't mind the workaround.

     
  • Anobium

    Anobium - 2021-07-05

    I must sort the silent failure with an error message. :=)

    Give me a few more mins.

     
  • ToniG

    ToniG - 2021-07-05

    I copied the 4 patch files to the GreatCowBasic folder.
    Inspecting messages.dat the following change...
    NotABitORAConst = "%value% is not a bit or valid constant. Constant values should be 0 or 1"

    Result: compiles the same with no error/warning message.

    Is this error message just until the issue is fixed or is the issue not fixable.
    thanks

    Index = 3
    A1=0
    Data1 = Valu(A1)      '< Variable A1 works here
    Data1.Index = Valu(A1).Index    ' < but not here
    
     
  • Anobium

    Anobium - 2021-07-06

    @ToniG

    Download the msg file again. I failed to upload the correct file.


    What is status of fix ? You will get an error message when you try the array(element).bit.

    Is this fixable ? Yes. It would take time. Got time to resolve?

    The fix is relatively simple but hard to implement. We never tested this use case. An oversight - yes.

    The use case.

    Data1.Index = Valu(A1).Index
    

    Needs to be evaluated as follows: This is NOT happening at the moment as the compiler does not handle array(element).bit correctly as the array pointer is being passed not the bit value, see later.

    'Data1.Index = Valu(A1).Index
    
    tmpsysvarN = Valu(A1)
    tmpsysvarN.0 = tmpsysvarN.Index
    Data1.Index = tmpsysvarX.0 
    
    or, maybe this would work..would need testing
    
    tmpsysvarN = Valu(A1)
    Data1.Index = tmpsysvarN.Index
    

    Currently, the compile is passing the array pointer ( INDF.index ). Where INDF is a pointer the array element. Both the source and destination are 'bits' but INDF.index is incorrect - hence the silent failure.

    Code being processed by the compiler - the source bit is not valid.
    Data1.Index = INDF.Index


    This is my analysis of the root cause.

    Evan

     

    Last edit: Anobium 2021-07-06
  • Anobium

    Anobium - 2021-07-06

    @ToniG. I have uploaded a ZIP today. Please please on top of the released code. See https://sourceforge.net/projects/gcbasic/files/Release%20Candidates/Patches/

    I just checked the release log, This is the second change to bit setting code. 984, 985 and now 993. The bit setting code is new and clearly improvements are still required. :-)

     
    • Anobium

      Anobium - 2021-07-06

      @ToniG.

      A thought... I am guessing that assigning an array(element).bit also fails, and, it fails silently.

      Valu(A1).Index = 1              '<  Probably not working.. not tested but if the array(element).bit as source fails... so, will this.
      

      I am out for a day or two - can you test the code shown above?

       
  • ToniG

    ToniG - 2021-07-06

    Thanks for the detail, it helps to understand.
    The workaround is ok, but it could be documented somewhere for now.
    My use is I have a For loop that sets each array element number & a nested loop that gets the bits of each element to place in D0 of a 32x4 RAM in a display controller. (1 bit set in 32 address)

    Pretty sure it works the other way around (setting array bits using a variable to set element number), this is what I mentioned earlier.
    But I will retest & confirm...

    What I tested yesterday:

        Data1.0= Valu(A1).Index  '<  not werking
        Data2.0 = Valu(0).Index   '<  ok
       Valu(A1).Index = Data1.0   '<  ok
       Valu(0).Index = Data1.0   '<  ok
    

    where index = 0 to 7


    Considering the only difference being the element number source, (Constant or variable)
    maybe as a interim fix can "trick" the compiler section to think it has a constant when the element number source is actually a variable .
    (hypothetically speaking & not knowing the actual code yet...)

     
    • Anobium

      Anobium - 2021-07-06

      ToniG. Not sure what display this is.. but, this method is slow.

      I am not saying we should not resolve this silent failure.

      But, use rotate with a constant will be much, much faster.

      Which/type of display is this? I can point you to some faster code.

       
  • ToniG

    ToniG - 2021-07-06

    Its a HT1621 driver chip. its a 128 element 4 COM LCD driver & for now I am just using 1 COM hence only D0 of each 4 bit RAM location. 1 bit per RAM address.
    The array is the 7seg digit values, 4 array elements per COM.
    Getting bits from the array to port pin (serial out) is fast enough for this app, the slow part is the BCD convert (MOD)

     
  • ToniG

    ToniG - 2021-07-06

    You were right Evan, It does fail both ways ( a false memory or confused when testing.)

    For Index = 0 to 7
        '   Bits FROM Array
        Data1.Index = Arr_Val(E0).Index   '<  not werking
        Data2.Index = Arr_Val(0).Index    '<  ok
    
        '   Bits TO Array
        Arr_Val(E1).Index = Const1.Index  '<  not werking
        Arr_Val(2).Index = Const1.Index   '<  ok
      Next
    

    I copied the latest patch files but still no error or warning message

    Test code attached

     
  • Anobium

    Anobium - 2021-07-07

    I have resolved. The compiler will now expand the array(element).bit addressing to handle.

    ToniG - I will post a new compiler later. 0.98.07 2021-07-07 (Windows 64 bit) : Build 995

    If you do not get the build 995 then re-examine your installation method of the patches.

     
  • Anobium

    Anobium - 2021-07-07

    @ToniG

    I have resolved but there are side effects and I want to discuss.

    We have three cases to resolve.

    1) datavar1.Index = Valu(A1).Index

    This is where the source data (right hand side of the equals sign is of the format arrayname(element_number_variable).variable_as_bit_address.

              ' datavar1.Index = Valu(A1).Index  can be expanded to
              '          SYSARRAYELEMENTCOPY = Valu(A1)
              '          datavar1.Index = SYSARRAYELEMENTCOPY.Index
    

    This works ok but I have NO way of determining if .variable_as_bit_address is a constant. Processing for constants happens a lot later in the compilation process and this means that arrayname(element_number_variable).constant_as_bit_address is also expanded where this work without expansion in the past.

    2) There a huge number of cases where datavar1.Index = Valu(A1).Index can have a prefix like IF, ELSEIF, WHILE etc. And, the expansion process would need address these issues.

    3) There is a case of the setting the destination. Example follows:

            'Valu(A1).Index = 1
            'expands to
            '          SYSARRAYELEMENTCOPY = Valu(A1)
            '          SYSARRAYELEMENTCOPY.Index  = source
            '          Valu(A1) =  SYSARRAYELEMENTCOPY
    

    This essentially will expand, but the same issues with IF.. WHILE will hamper this.

    4) These is a fourth case of copying bits from one array to another.

            'ValueA(A1).Index = ValueB(A1).Index
    

    The expansion approach fails on this case. There is not expansion logic I can think design that works.


    So, I recommend NOT implementing the expansion but leave the resolution up to the user. The compiler will issue an error, and, we can document how to resolve. This will remove the risk of expansion messing something up and it is very simple for a user.

              ' datavar1.Index = Valu(A1).Index  can be expanded to
              '          SYSARRAYELEMENTCOPY = Valu(A1) 'read the array value
              '          datavar1.Index = SYSARRAYELEMENTCOPY.Index
    

    and

            'Valu(A1).Index = source (or any variable binary value 1 or 0 ) 
            'expands to
            '          SYSARRAYELEMENTCOPY = Valu(A1)  'read the array value
            '          SYSARRAYELEMENTCOPY.Index  = source
            '          Valu(A1) =  SYSARRAYELEMENTCOPY
    

    The expansion is way to complex, and, I do not like complex. I would recommend we leave it up to the user.

    Your thoughts?

     

    Last edit: Anobium 2021-07-07
  • ToniG

    ToniG - 2021-07-07

    For the patch updates I just renamed the original 4 files with *.bak & copied over the new files & run application as normal. I do have the minimal install (I don't use microchip's monster)
    My dev pc(win7p_x64) is not networked, UAC disabled.
    I did have some trouble with MSE quarantining gcbasic.exe but got around it.


    Best to avoid complex, unless it can be isolated.
    As can be seen in the gcb test it is just 1 extra statement to mitigate , Warning message & a mention in help will be useful.
    My use is to set just 1 bit in a variable from array element bits .

         Data1.0 = Digit_Val(LP2).idx2 
        ' becomes...
        tmp1 = Digit_Val(LP2): Data1.0 = tmp1.idx2
    
     
  • Anobium

    Anobium - 2021-07-07

    OK. I will upload in my morning.London time.

     
    • Anobium

      Anobium - 2021-07-08

      Uploaded https://sourceforge.net/projects/gcbasic/files/Release%20Candidates/Patches/

      Please use the ZIP. Then, check the version and build. You should see build 995

      0.98.<<>> 2021-07-07 (Windows 64 bit) : Build 995

      Two new traps to prevent the silent compilation that does not work.

      ArrayBitIndex_1.gcb (23): Error: Array(element).bit as source - compiler needs help. Please simplify 'Array(element).bit' by using a temp_variable to store the source Array(element) then use temp_variable.bit as source, or use Constant array element address.

      ArrayBitIndex_1.gcb (37): Error: Array(element).bit as destination - compiler needs help. Please simplify destinationn 'Array(element).bit' by using a temp_variable to store the destination Array(element) then use temp_variable.bit as destination, or use Constant array element address.

       

      Last edit: Anobium 2021-07-08
  • ToniG

    ToniG - 2021-07-08

    Below is my use of the array bit indexing, the temp var sits quite well in the code.
    The 16 array elements are a memory map of the HT1621 32x4 mem.

    Dim Digit_Val(16) as Byte
    set_all:
                ' mem address is auto increment in 1621 hw 
        idx2 = 0
        cs = 0
        Wait 10 us
      For LP2 = 0 To 3    '<  start of 1621 mem addr block(0, 8, 16, 24)
        Do While idx2 < 8  
          tmp1 = Digit_Val(LP2):    Data1.0 = tmp1.idx2   ' COM0
          tmp1 = Digit_Val(LP2+4):  Data1.1 = tmp1.idx2   ' COM1
          tmp1 = Digit_Val(LP2+8):  Data1.2 = tmp1.idx2   ' COM2
          tmp1 = Digit_Val(LP2+12): Data1.3 = tmp1.idx2   ' COM3
          idx2 = idx2 +1
          Gosub Send_Mem '< send 4 bits Data1 serialy
        Loop
          idx2 = 0
      Next LP2
        cs = 1
    Return
    
     
    • Anobium

      Anobium - 2021-07-08

      Very nice.

      Can you try the updated compiler to ensure I have sorted the error messages ( not the expansion, I have coded but I have left the expansion code disabled ).

      :-(

       

      Last edit: Anobium 2021-07-08
  • ToniG

    ToniG - 2021-07-08

    sorry, I have to cleanup my GCB install.
    With the latest release I installed to a different folder (Keeping the previous)
    Looks like the old install compiler is being run?. (Compiler Version: 0.98.07 RC45 2021-03-31 (Windows 32 bit)

    That's why I don't see the messages...

    (After I installed latest build I no longer got a hex file output, I had to copy the old makehex.bat from the previous install.)

     
    • Anobium

      Anobium - 2021-07-08

      I would do a temp install and then dump the patches on top.

      Thank you

       
  • ToniG

    ToniG - 2021-07-08

    ok, all sorted now.
    With a fresh install of latest & a patch dump, hex button was still compiling with old compiler 2021-03-31 gcbasic.exe & no messages
    Edit makehex.bat...
    REM gcbasic.exe /NP /S:use.ini %1 /F:N /P:
    gcbasic64.exe /NP /S:use.ini %1 /F:N /P:

    That's better, now I get in errors .txt with

    22    Valu(A1).Index = Data1.Index
    23    Data1.Index = Valu(A1).Index 
    
    ArrayBitIndex_1.gcb (22): Error: Array(element).bit as destination - compiler needs help.  Please simplify destinationn 'Array(element).bit' by using a temp_variable to store the destination Array(element) then use temp_variable.bit as destination, or use Constant array element address.
    ArrayBitIndex_1.gcb (23): Error: Array(element).bit as source - compiler needs help.  Please simplify 'Array(element).bit' by using a temp_variable to store the source Array(element) then use temp_variable.bit as source, or use Constant array element address.
    
     
1 2 > >> (Page 1 of 2)

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.