CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: ANSI C: Implementing a stack machine natively working with dynamic strings?

  1. #1
    Join Date
    Oct 2004

    ANSI C: Implementing a stack machine natively working with dynamic strings?

    Hello folks,

    for a scripting language I'm currently designing, I wanted to implement a stack-based virtual machine for executing the scripts in their internal representation (code for this stack-machine). This stack-machine should natively support string operations like catenations directly on the stack, so all stack elements will be pointers of char.

    My thoughts where, that pointers to values of variables and constants (which are stored within a constants table) are directly pushed to the stack, and temporarily needed values are provided with a flag that differs between temporary allocated strings and "fixed" pointers from the variable-system or constant system (variable pointers are changed by STORE-operations only!).

    So with this requests, I got the following structure declaration for a stack element.

    struct _operand
        unsigned is_malloced:1;
        char*    p;
    I know that this virtual machine will not be the most performant one, and I already have some ideas to improove this performance, for example directly pushing the atof()'d values of strings to the stack before mathematical operations are invoked, but that will be an optimization process of the compiler. Another type of optimization could be that temporary allocated memory is cached, to be re-used at a later, temporary variable allocation.

    My question is now: Is there already an implementation of such a machine somewhere, maybe with a good performance? Or do you generally advise against this idea? Are there alternatives? The problem is, that my scripting language is a very flexible, typeless language. It ONLY knows the "datatype" dynamic string, and to work with numbers, strings are internally converted to numbers for calculation. So I thought of implementing a stack machine with this very high abstraction level, to support all the dynamic needs the language goals, in a professional and stable manner.

    I would be glad if someone can help me. Thanks for your help in advance!


  2. #2
    Join Date
    May 2008

    Re: ANSI C: Implementing a stack machine natively working with dynamic strings?

    That seems reasonable.
    Your flag name fooled me for a minute. In addition to indicating whether or not the string is dynamically allocated, you should also indicate whether or not it is a temporary that should be deleted by the caller.

    For example, to concatenate two strings:
    // script language!
    1: foo = "Hello"
    2: baz = foo + " world!"
    in pseudo
    (line 1)
    PUSH foo, dynamically allocated, don't delete me
    PUSH " world!", dynamically allocated, ok to delete me
    CALL +
    (+ function)
    allocate result
    copy result [SP-2] (foo)
    append result [SP-1] (" world!")
    POP and FREE
    POP (only)
    PUSH result, dynamically allocated, ok to delete me
    (line 2)
    assign baz [SP-1]
    POP (only)

    In words
    So foo is pushed onto the stack as "dynamically allocated, but DON'T delete me",

    and " world!" is pushed onto the stack as "dynamically allocated, and ok to delete me".

    The + routine gets the two stack arguments, allocates another temporary large enough to store the concatenated string, copies the data into it, pops and frees, pops only, and pushes the result as "dynamically allocated, and ok to delete me".

    The caller gets it back, pops it and assigns the result to baz, toggling "ok to delete me" to "DON'T delete me".

    Hope this makes sense... and helps. :-\ :-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)