Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > sci.electronics.design > #466019
| From | Don Y <blockedofcourse@foo.invalid> |
|---|---|
| Newsgroups | sci.electronics.design |
| Subject | Re: Hardware scripting (a better Arduino) |
| Date | 2017-06-28 10:51 -0700 |
| Organization | A noiseless patient Spider |
| Message-ID | <oj0q38$vb8$1@dont-email.me> (permalink) |
| References | <LEt4B.72469$9S6.22388@fx02.iad> <BdD4B.56201$bO5.20256@fx16.iad> <eEP4B.94365$AG1.6246@fx42.iad> |
On 6/28/2017 8:08 AM, bitrex wrote:
> I haven't had a chance to follow up to each reply to this thread individually
> today, so I'll just use the opportunity to address a couple of the objections
> "Rob" and "Don Y" mentioned here...the performance of the stack machine doesn't
> have to be particularly great, as profiling usually shows that most apps spend
> the vast majority of their time grinding thru the same small bit of code,
> perhaps only 5% of the total codebase.
Its not the amount/percentage of time that is spent "less efficiently"
(avoiding the term "INefficiently") processing the code but, rather,
WHICH code is processed "less efficiently". I found that in the designs
that I did (e.g., the '7180 mentioned), I still had to manually code
a stub ISR to handle the issues where timeliness was important -- because
the interpreted code couldn't guarantee that it would get around to executing
in the timeframe available.
OTOH, the stubs could invoke code written in this bogo-language -- *if* they
were otherwise constrained in frequency, etc.
[Note part of this was due to the way I implemented the multitasking executive
to expressly eliminate the need for explicit atomic regions and synchronization
primitives. Unlike a preemptive context switch, you were largely at the
mercy of whatever code happened to be executing at that instant.]
> In an embedded app this might be say
> some signal processing or other data-cruching code, or handling and dispatching
> packets coming in over a network. Like in the games analogy, that area is the
> equivalent of the "engine" that's doing the graphics, and would certainly be at
> least implemented in C/C++.
>
> It will get its work done nearly as fast if the algorithm is simply referenced
> by some function pointer on a software stack that gets called with other stack
> objects popped into it, as that's essentially how calling functions on local
> variables from procedural code works with the intrinsic processor stack defined
> by the uP's ISA which a C/C++ compiler uses, and with a simple architecture
> that lacks virtual memoy, cache, TLBs, and all that stuff a well-implemented
> software stack should be nearly as fast.
This is the approach I've taken with my current project: all of the
time/performance-critical services are written in C/ASM while the higher
levels of abstraction are written in a scripting language. The latter
deprives the developer of the ability to fret over performance issues
and concentrate, instead, on what he wants to get *done*.
E.g., its easy to see how the "lamp flashing" code presented earlier
can be reworked to provide a timeout on the data requested from the
user by the prompt (PRINT) that followed in that example. Does the
developer really care if each iteration of the FLASH loop is
exactly 500ms? Or, that they are all consistent? (obviously, you
could write such a loop where that was a primary goal -- but, it typically
won't be so why worry about those details of minimizing jitter, etc.?)
> Much of a codebase wont be performance-critical and using C++ for the rest of
> it is the proverbial using a sledgehammer to swat a fly.
>>> And then the logic for how the external hardware interacts could be
>>> written in a very abstract stack machine language, similar to Forth or
>>> PostScript, or a programmable RPN calculator. An example stack machine
>>> implementation for Arduino I saw would blink an LED on and off like this:
>>>
>>> 13 output
>>> {
>>> 13 digitalToggle 1000 delay
>>> true
>>> } while
>>>
>>> These scripts aren't compiled, but actually stored as plaintext strings
>>> (in compressed mnemonic form) in Flash or EEPROM and then interpreted on
>>> the fly by the stack machine.
>>
>> This is where I diverge. "Compressed mnemonic" means tokenised. It's
>> better to replace each opcode by a direct pointer to the function
>> that implements the opcode, aka a threaded interpreter. You use a
>> pointer register to replace the PC, so execution is just asm("jmp p++"),
>> and the function extracts its parameters using "*p++". Trivial to
>> implement, and very fast on a not-heavily pipelined CPU.
>
> Yeah, you could have each "opcode" represented by a single unsigned char, so
> with an RPN-kind of language the "interpreter" works pretty much the same as
> you describe, a program counter stepping thru the script and pushing and
> popping functions and data on and off the stack, and harvesting return values,
> as appropriate.
Exactly. You use the fetched "bytecode" as an index into a list of
function pointers and invoke the function referenced. To it, you pass
a pointer into the "instruction stream" so it can fetch "arguments" from
the instruction stream, returning the updated "PC" for use by the next
iteration of the interpreter.
> You can have foolproof variant types that will happily hold both function
> pointers and data in the same "package", on the same software stack, using a
> custom stack allocator designed as appropriate for the capabilities of the
> hardware. You can have locally scoped lambda functions generated on-the-fly at
> compile time and stored in flash memory that can be templated to accept any
> number of arguments from the stack as is required automatically.
"Typing" is something that requires careful thought. Often, you don't *need*
a variety of types: a numeric type and a string type can cover a lot of
applications. More types inevitably lead to more conversions between types,
formatted printing, etc. My scripting language uses one of each and allows
them to "grow", as dictated by the application.
The advantage, there, is that the user need not be concerned with things
like overflow or ordering operations to maximize preserved precision, etc.
The disadvantage is that you do a lot *for* them and take away a fair
bit of control that they might want to exert on the performance of their
code.
[It also means you tend to need runtime GC -- which brings up another
variable in performance (as well as more implementation decisions)]
At the other end of the spectrum, you can constrain the language to
integer/fixed-point numerics and preallocated strings (buffers).
> All these things are standard stuff it the modern C++ hotness, and compile down
> to having not much more overhead than pure C or asm.
IME, the bigger issue was having a "development environment" that was easy to
explain to others. As I typically am involved in "proof of concept" efforts,
I want to be able to show something close to pseudo-code to the client and
the folks who will ultimately be charged with bringing the product to
manufacturing and then, the market. I don't want to have to "translate"
an application back into a *less* capable expressive form than the prototype
implementation just to show what it's doing (in detail). I'd rather present
the prototype in a more abstracted language and let them refine the efficiency
of the implementation to suit their goals (resources, timeliness, etc.)
As such, the language wants to be intuitive and not necessarily complex,
even if this comes at the expense of some functionality (e.g., I can
craft far more efficient code if I have hooks into the MTOS, use of pointer
variables, etc. -- but this makes the resulting code look less like
"generic pseudocode" and more like a particular implementation).
Consider the audience (Arduino) and what they're likely to call upon their
"boxes" to do. Will they be scanning raw video from a reflective photodetector
to decide barcodes? Or, accepting decoded barcodes (from something) and
manipulating them at some higher level...?
Back to sci.electronics.design | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Hardware scripting (a better Arduino) bitrex <bitrex@de.lete.earthlink.net> - 2017-06-27 10:07 -0400
Re: Hardware scripting (a better Arduino) Rob <nomail@example.com> - 2017-06-27 16:25 +0000
Re: Hardware scripting (a better Arduino) Don Y <blockedofcourse@foo.invalid> - 2017-06-27 10:12 -0700
Re: Hardware scripting (a better Arduino) Clifford Heath <no.spam@please.net> - 2017-06-28 11:01 +1000
Re: Hardware scripting (a better Arduino) bitrex <bitrex@de.lete.earthlink.net> - 2017-06-28 11:08 -0400
Re: Hardware scripting (a better Arduino) Don Y <blockedofcourse@foo.invalid> - 2017-06-28 10:51 -0700
Re: Hardware scripting (a better Arduino) bitrex <bitrex@de.lete.earthlink.net> - 2017-06-28 15:56 -0400
Re: Hardware scripting (a better Arduino) Don Y <blockedofcourse@foo.invalid> - 2017-06-28 14:04 -0700
Re: Hardware scripting (a better Arduino) Don Y <blockedofcourse@foo.invalid> - 2017-06-28 22:15 -0700
Re: Hardware scripting (a better Arduino) Don Y <blockedofcourse@foo.invalid> - 2017-06-30 12:23 -0700
Re: Hardware scripting (a better Arduino) David Brown <david.brown@hesbynett.no> - 2017-06-30 10:43 +0200
Re: Hardware scripting (a better Arduino) Clifford Heath <no.spam@please.net> - 2017-06-29 09:52 +1000
Re: Hardware scripting (a better Arduino) Clifford Heath <no.spam@please.net> - 2017-06-30 16:49 +1000
Re: Hardware scripting (a better Arduino) Clifford Heath <no.spam@please.net> - 2017-06-29 09:42 +1000
Re: Hardware scripting (a better Arduino) Tom Gardner <spamjunk@blueyonder.co.uk> - 2017-06-28 16:30 +0100
Re: Hardware scripting (a better Arduino) Clifford Heath <no.spam@please.net> - 2017-06-29 09:55 +1000
Re: Hardware scripting (a better Arduino) Les Cargill <lcargill99@comcast.com> - 2017-07-02 12:50 -0500
csiph-web