Shop OBEX P1 Docs P2 Docs Learn Events
clib - C library written in spin — Parallax Forums

clib - C library written in spin

Dave HeinDave Hein Posts: 6,347
edited 2010-03-24 13:12 in Propeller 1
I'm almost ready to post clib to the OBEX, but I want to have other people try it before I post it.· clib provide some of the standard C functions to spin programs.· It contains string functions, such as strcpy, strcat, strcmp and strncmp.· It also provides formated I/O with functions like printf, sprintf, scanf and sscanf.· In C, these functions take a variable number of parameters.· I accomodate this in spin by providing different versions of the functions for different numbers of parameters, such as printf0, printf1, etc.· I also provide a vprintf function that passes parameters in an array.

I include an object that provides floating-point I/O.· The floating-point I/O·object doesn't require the use of the floating point objects that are in the OBEX.· There are four references to the floating-point I/O routines in clib.spin that can be commented out to save space if needed.· clib also contains the malloc and free functions for dynamic memory allocation.· I use a simple linked-list method that only· incurs a 4-byte overhead for each allocated memory block.

I was originally going to use FullDuplexSerial with clib, but I ran into some issues with the variables being in a VAR section.· Since I needed to make some modifications I decided to add other enhancements.· All of the variables are now stored in a user defined array.· A pointer to the array is passed to the rx, tx and other functions.· This allows for multiple serial ports to be created, and they can be accessed by any object or any cog.· In addition, the sizes of the rx and tx buffers are passed as independent parameters.

The attached zip file contains a test program called testclib.spin.· It calls many of the functions in clib.spin, and provide examples of how they are used.· Please give clib a try, and let me know·if you think I should add any other functions or make any changes.·



Post Edited (Dave Hein) : 3/24/2010 3:42:41 AM GMT

Comments

  • pullmollpullmoll Posts: 817
    edited 2010-03-24 04:36
    Dave Hein said...

    I was originally going to use FullDuplexSerial with clib, but I ran into some issues with the variables being in a VAR section. Since I needed to make some modifications I decided to add other enhancements. All of the variables are now stored in a user defined array. A pointer to the array is passed to the rx, tx and other functions. This allows for multiple serial ports to be created, and they can be accessed by any object or any cog. In addition, the sizes of the rx and tx buffers are passed as independent parameters.

    Hi Dave,

    this looks very useful to me. I may use your version for my purposes, too [noparse]:)[/noparse]
    Dave Hein said...
    The attached zip file contains a test program called testclib.spin. It calls many of the functions in clib.spin, and provide examples of how they are used. Please give clib a try, and let me know if you think I should add any other functions or make any changes.

    Since I'm always tempted to write if !strcmp(a,b) your lib could ease my life smile.gif

    Juergen

    Edit: While looking through clib.spin I saw the malloc() implementation and the line after ' Split block if larger than needed. I think you may be destroying the free list in the corner case when the block is just 1,2 or 3 greater than the requested size?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    He died at the console of hunger and thirst.
    Next day he was buried. Face down, nine edge first.

    Post Edited (pullmoll) : 3/24/2010 5:07:14 AM GMT
  • Dr_AculaDr_Acula Posts: 5,484
    edited 2010-03-24 04:37
    That is quite intriguing. It is a hybrid language of c and spin and to my eye, combines the strengths of both languages.

    I wonder how far you could take the concept? Brainstorming here, but I've always liked the { and } in C as I think it is more explicit than the indents in spin, especially when you copy and paste a spin program via notepad and it loses the indenting. Could compilers handle { and treat it as indents until it gets a }.

    You could always get the same effect by writing '{ and '} and the compiler would ignore those lines, but the program would look more familiar to those who are used to C.

    There could be some advantages to passing variables in arrays rather than in a VAR section, particularly when building cog code that is portable and can be saved as a binary file on an sd card and loaded in and out as required.

    Have you got and more c. instructions in the pipeline?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    www.smarthome.viviti.com/propeller
  • pullmollpullmoll Posts: 817
    edited 2010-03-24 05:08
    Dr_Acula said...
    That is quite intriguing. It is a hybrid language of c and spin and to my eye, combines the strengths of both languages.

    I wonder how far you could take the concept? Brainstorming here, but I've always liked the { and } in C as I think it is more explicit than the indents in spin, especially when you copy and paste a spin program via notepad and it loses the indenting. Could compilers handle { and treat it as indents until it gets a }.

    You could always get the same effect by writing '{ and '} and the compiler would ignore those lines, but the program would look more familiar to those who are used to C.

    There could be some advantages to passing variables in arrays rather than in a VAR section, particularly when building cog code that is portable and can be saved as a binary file on an sd card and loaded in and out as required.

    Have you got and more c. instructions in the pipeline?

    Instead of breaking existing compilers [noparse]:)[/noparse] you could just as well write a C compiler that emits Spin code rather than PASM. The output would just perhaps not be the fastest code that is possible in Spin.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    He died at the console of hunger and thirst.
    Next day he was buried. Face down, nine edge first.
  • Dave HeinDave Hein Posts: 6,347
    edited 2010-03-24 13:12
    Juergen and Dr_Acula,

    Thanks for your comments.· Yes, there does seem to be a problem when splitting blocks in the malloc routine.· I'm not allowing for the·memory required to add another header, or even useable space after that.· I'll change the conditional expression to "if (word[noparse][[/noparse]currblk][noparse][[/noparse]BLK_SIZE] > size + HDR_SIZE + 3)".· This will allow enough memory for an additional header plus at least 4 bytes of memory space.

    I'll probably add a few more·string routines.· These are·easy to write, and they don't take much space.· I would appreciate suggestions on which functions I should add.· I'm also going to add an option to use a lock with the serial I/O so it will·be·safe to use·from multiple cogs.

    I do have a couple of other projects for enhancing spin and converting C to spin.· They are called xspin and cspin.· With xspin I will add the C pre-processor to provide defines, macros, includes and conditional compile.· It also has special sections called CVAR, CPUB and CPRI, which stands for Cog VAR, PUB and PRI.· This will generate assembly code from spin that runs in a Cog.· It will have a FLOAT variable type, and automatically generate floating-point expressions.

    cspin will convert C code to spin.· I'm about half done with this project, and I have used it several times to convert open C source to spin.· It understands some of the standard C functions, and will automatically generate calls into the C library.· It will expand printf("x = %d\n", x) into c.printf1(string("x = %d\n"), x)

    Dave

    ·
Sign In or Register to comment.