Shop OBEX P1 Docs P2 Docs Learn Events
flexspin compiler for P2: Assembly, Spin, BASIC, and C in one compiler - Page 84 — Parallax Forums

flexspin compiler for P2: Assembly, Spin, BASIC, and C in one compiler

18182848687122

Comments

  • @Rayman said:
    Thanks @Wuerfel_21 , I suppose "struct" would be better for Spin conversions then.

    class is closer, since in standard Spin you can't just access variables of an object. Flexspin allows it, anyways. Not sure if FlexC even has a concept of variable visibility though.

  • evanhevanh Posts: 15,917
    edited 2021-05-29 03:31

    Eric,
    I just bumped into a couple of related assembler bugs with inc/dec PTRx encoding:

    • First one is a zero inc/dec doesn't generate an error when it should, but instead encodes a value of one. eg:
    0009c 027 40 04 D8 FC |         rep @.rend1, pin_count
    000a0 028 41 7E 88 F9 |         altd    pin_num, level_base     ' pin_num++
    000a4 029 61 01 34 FC |         wrlut   0-0, ptra++[0]          ' distribute latest samples from "adc_levels" to lutRAM buffers
    000a8 02a             | .rend1
    
    • Second one is an inc/dec of 16 incorrectly generates an error even though it correctly encodes a value of zero (which represents 16) in the .lst file. eg:
    Propeller Spin/PASM Compiler 'FlexSpin' (c) 2011-2021 Total Spectrum Software Inc.
    Version 5.5.0 Compiled on: May 29 2021
    adc-to-millivolts2.spin2
    adc-to-millivolts2.spin2
    adc-to-millivolts2.spin2:174: error: ptr index out of range -32 to 31
    
    0009c 027 40 04 D8 FC |         rep @.rend1, pin_count
    000a0 028 41 7E 88 F9 |         altd    pin_num, level_base     ' pin_num++
    000a4 029 60 01 34 FC |         wrlut   0-0, ptra++[16]         ' distribute latest samples from "adc_levels" to lutRAM buffers
    000a8 02a             | .rend1
    
  • @ersmith I bumped into a couple of minor warnings that are new with 5.5.0 in the file system.

    d:/Flex2Gui/flexgui/include/filesys/fatfs/ff.cc:3910: warning: rcnt is possibly used before initialization
    d:/Flex2Gui/flexgui/include/filesys/fatfs/ff.cc:4014: warning: wcnt is possibly used before initialization
    d:/Flex2Gui/flexgui/include/filesys/fatfs/ff.cc:5801: warning: n_lba32 is possibly used before initialization
    d:/Flex2Gui/flexgui/include/filesys/fatfs/ff.cc:6917: warning: f is possibly used before initialization
    d:/Flex2Gui/flexgui/include/filesys/fatfs/ff.cc:6948: warning: f is possibly used before initialization
    d:/Flex2Gui/flexgui/include/filesys/fatfs/ff.cc:7172: warning: f is possibly used before initialization
    

    Again, these are just warnings and everything compiles fine, but I figured you might want to know.

  • Is there a non-blocking way to grab a single character from STDIN in FlexBASIC? I'm trying to implement the equivalent of INKEY$ (return a single char from input if one is present, but if there is nothing present then return zero without waiting). Unfortunately everything I have tried results in blocking. I could do this by dedicating this function to a cog, but that seems wasteful. In Windows there was the KBHIT function, but it's not part of the standard lib and isn't portable. Suggestions?

  • RaymanRayman Posts: 14,648

    @JRoark
    In Flex C it's like this:
    cmd2 = _rxraw(1);//rja using this to avoid blocking with getchar()

  • JRoarkJRoark Posts: 1,215
    edited 2021-05-31 15:55

    @rogloh Thank you! That was exactly what I was looking for. And (hangs head in shame), it's even in the GENERAL.PDF doc:

    int _rxraw(int n=0)
    
    Receives a character on the default serial port. n is a timeout in milliseconds. If
    the timeout elapses with no character received, _rxraw returns -1, otherwise it
    returns the received character. The default timeout (0) signifies "forever"; that
    is, if n is 0 the _rxraw function will wait as long as necessary until a character
    is received.
    
  • Wuerfel_21Wuerfel_21 Posts: 5,054
    edited 2021-06-01 00:07

    If you haven't heard of it yet, I've been working for like a week now on adding bytecode support to flexspin. Right now it only targets Spin1 on P1 and is very alpha, but it's supposed(tm) to eventually(tm) support Spin2 on P2. C and BASIC maybe on P2, too, but that'd be low priority.

    This is pretty important since the only other optimizing Spin1 compiler is abandonware and there aren't any other Spin2 compilers than PNut and flexspin.

    Anyways, I've attached a build of the bytecode-enabled flexspin if you want to toy around with it for some reason.

    • keep in mind that it is only supposed to work with Spin code on P1
    • pass --interp=rom to enable bytecode mode
    • use -O1 to enable the odd optimization. -O2 is broken.
    • There's loads of diagnostic output right now, so when building a large program, best redirect it to a file and search it for "error".
  • Just WOW.

    very cool @Wuerfel_21

    Mike

  • aaaaaaaarghaaaaaaaargh Posts: 82
    edited 2021-06-01 13:46

    Hi all, I really love flexbasic and Notepad++, so I'm using NPP+ as IDE for flexbasic (and flexC).
    @Fred asked me how to set this up, so I though maybe this could be useful to others as well, so here goes:

    Using this info you will have the following features in Notepad++

    • Flexbasic syntax colorization
    • Flexbasic code folding (for Loops, IF/THEN, Subs and Functions)
    • A Function List for Subs and Functions
    • Brace Matching and Bad Brace Colorization
    • flexbasic syntax check, compilation and propeller upload inside NPP (will also work with flexspin and flexC)
    • compiler console output with clickable text to jump to error line

    Most of that will work also work for flexC as NPP+ has C support built in by default

    This is what it looks like:

    This NPP_flexbasic01.ZIP file contains all needed files.

    This is of course all just a little info to get you going... customization to fit your environment is definitely needed ;-)

    Installation:
    Install a recent version Notepad++ I have tested with NPP 7.9.3 and 7.95 - Old versions have a different way of doing syntax colorization!
    Install the NPPEXEC addon using the plugin admin
    make "functionList" directory in the NPP Appdata folder:
    c:\Users\YOUR_USER_ID\AppData\Roaming\Notepad++\functionList\
    Remember the Appdata folder might be hidden!
    Copy FlexBasic.xml and overrideMap.xml from the ZIP file to the new folder
    in NPP import a new User Define Language:
    Menu Language -> User Defined Language -> Define you languange
    Press "Import" and select flex.xml (from the ZIP file)
    Close the dialog and Close Notepad++
    Now start Notepad++ again and open a flexbasic (.bas) file.
    Set the file's language to "Flex"
    Menu Language -> Flex (towards the bottom of the menu)
    Open the Function List: Menu -> View -> Function List
    Now you have all editor features.
    To compile or check syntax from NPP we'll use NPPEXEC.
    If have assigned 2 Function keys for this using the Shortcut Mapper:
    F5 for compilation and upload to propeller, F6 for flexbasic syntax check - but you can define any menu entry or key combination you wish instead
    Basically NPPEXEC runs a little script to save all files that are open in NPP and the runs a batch file to compile or check syntax, the commands are just copied out of the flexprop gui. Before uploading a taskkill command is issued to stop all running instances of proploader.exe (P1) or loadp2.exe (P2) so you can just Press compile&run wihout having to close any open loader/output windows.
    You will have to change the com port and the (temp) paths to fit your environment.
    The P1 folder contains batch files for the propeller 1 , the P2 folder contains batch files for the propeller 2.
    The Zip files contains a couple of screenshots on how to configure NPPEXEC so that errors are clickable and highligted and how to map the shortcut Keys

    I hope it'll work for you...

  • @evanh : Thanks for finding those ptra++ bugs. I've fixed them in github now.

    @JRoark : Looks like the checks for uninitialized variables were missing some cases. These warnings are fixed in github, thanks.

    And everyone, @Wuerfel_21 has been doing some very cool things with the bytecode generation. I hope we'll get that merged into the main distribution soon.

  • evanhevanh Posts: 15,917

    Thanks Eric,
    I'd amused myself and got by by copying that prior assembled code into a hand coded longword:

            rep @.rend1, #_pincount
            altd    pin_num, level_base     ' pin_num++
    '       wrlut   0-0, ptra++[16]         ' distribute latest samples from "adc_levels" to lutRAM buffers
            long    $FC34_0160      'temporary workaround for PTRA++ assembler bug
    .rend1
    
  • evanhevanh Posts: 15,917

    Oh, the red colour of the error messages is quite dim. I'm guessing there is a bold or bright red option that could be used?

  • Are you using a non-Windows OS? Because yeah, it seems that it doesn't use the bright colors there, ooops.

  • evanhevanh Posts: 15,917

    KDE's Konsole on Kubuntu 20.04.

  • I just submitted a PR that should fix it, try that.

  • evanhevanh Posts: 15,917
    edited 2021-06-01 22:51

    A what? Where?

    Ah, found it "Pull Request" - https://github.com/totalspectrum/spin2cpp/pull/146/commits/6bf1568f6f5370324c554048932e2159775f49ee

    No idea how to get it other than hand editing the source file. "git pull" doesn't work. I haven't really tried to learn GIT.

  • (I'm assuming you're building your own linux binaries, right?)

    A pull request. Clone https://github.com/Wuerfel21/spin2cpp.git and build from the patch-1 branch

  • evanhevanh Posts: 15,917
    edited 2021-06-01 22:58

    @Wuerfel_21 said:
    (I'm assuming you're building your own linux binaries, right?)

    Yep, I git pull the master branch, then make.

    A pull request. Clone https://github.com/Wuerfel21/spin2cpp.git and build from the patch-1 branch

    Oh, can I not get it from that submitted fix on totalspectrum?

  • @evanh said:

    @Wuerfel_21 said:
    (I'm assuming you're building your own linux binaries, right?)

    Yep, I git pull the master branch, then make.

    A pull request. Clone https://github.com/Wuerfel21/spin2cpp.git and build from the patch-1 branch

    Oh, can I not get it from that submitted fix on totalspectrum?

    Yes, until it's merged, you need to clone the fork originating it and then git checkout the-branch-you-want.

    Or just wait till Eric gets out of bed or whatever, I'm sure he'll merge the fix soon.

  • evanhevanh Posts: 15,917

    Hey, it works! :) Sadly, I'll not remember all that. Is there a way to list the branches?

  • git branch -a

    You can also have different “remotes”, meaning the places where git looks for changes and can pull from. This way you can easily switch between the upstream release or the branch Wuerfel is working on.

  • evanhevanh Posts: 15,917

    I see. That got me a big list. Took a while to realise the bulk of the list was all the "remotes". Thanks for the info.

  • How do I access the contents of absolute memory location and assign it to a ubyte? Example: I want to print the contents of hub ram starting at some user-specified offset such as 0300h.

    I'm chasing a memory corruption issue so I figured I'd just print-out a chunk of memory as a quick-and-dirty HEXDUMP util in FlexBASIC. This should be a 5 minute task, except that I am well and truly stuck. Buehler? Anyone? :)

  • Can’t you just use BYTE[address]? That works for me for e.g. a ring buffer in Hub Ram.

  • @JRoark said:
    How do I access the contents of absolute memory location and assign it to a ubyte? Example: I want to print the contents of hub ram starting at some user-specified offset such as 0300h.

    From the context I assume you're using BASIC, in which case you would use a ubyte pointer, something like:

    ' print memory starting at 0x2000
    var p = cast(ubyte pointer, 0x2000)
    for i = 0 to 10
      print hex$(p[i]); " ";
    next
    print
    
  • @ersmith That works a treat! I’d never seen CAST used like that. Simple.
    @deets Before Eric posted, thats pretty much what I did. I dropped into inline assembly and did it that way. Gotta love the Flex suite for that!

    Thanks to both of you! (I found my bug, btw)

  • I have a question that might be only semi-related, please let me know if you think I should post a new topic or point to existing documentation.

    I'm on the lookout for some code-organization patterns. I currently write code that decodes data coming over serial line as well as reading a FAT32 SD Card file lists, and in both cases I need to decode hex nibbles. This is easy enough done of course, a simple

    pri decode_nibble(c) : result
      result := c - $30 ' convert from ascii into 0..9
      if result > 9 ' we assume A-F
        result &= %1011111 ' enforce upper case ASCII
        result -= 7
    

    However I need this in several spin2 files. I can currently see two ways of sharing this code:

    • the ugly variant that I'm currently using - copy & paste the method into my other spin object.
    • encapsulation into my own object, but then use several instances of this.

    The latter is the one I'm ogling right now because that's what I would do in other languages. But I'm concerned for two unbeknown properties of this solution: code duplication in the binary, and object instantiation overhead. Does anybody know what happens in case I use several instances of essentially a pure functional library object?

  • dnalordnalor Posts: 222
    edited 2021-06-04 15:56

    nonsens deleted.

  • uhm, > @deets said:

    I have a question that might be only semi-related, please let me know if you think I should post a new topic or point to existing documentation.

    I'm on the lookout for some code-organization patterns. I currently write code that decodes data coming over serial line as well as reading a FAT32 SD Card file lists, and in both cases I need to decode hex nibbles. This is easy enough done of course, a simple

    pri decode_nibble(c) : result
      result := c - $30 ' convert from ascii into 0..9
      if result > 9 ' we assume A-F
        result &= %1011111 ' enforce upper case ASCII
        result -= 7
    

    However I need this in several spin2 files. I can currently see two ways of sharing this code:

    • the ugly variant that I'm currently using - copy & paste the method into my other spin object.
    • encapsulation into my own object, but then use several instances of this.

    The latter is the one I'm ogling right now because that's what I would do in other languages. But I'm concerned for two unbeknown properties of this solution: code duplication in the binary, and object instantiation overhead. Does anybody know what happens in case I use several instances of essentially a pure functional library object?

    If an object is used multiple times, only one instance of the code is compiled. If the object doesn't declare any variables, it is effectively free to instantiate it as often as you want. Also note that flexspin will not include any functions that aren't actually used.

  • @Wuerfel_21 thanks!

Sign In or Register to comment.