Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > sci.electronics.design > #465999

Re: Hardware scripting (a better Arduino)

Subject Re: Hardware scripting (a better Arduino)
Newsgroups sci.electronics.design
References <LEt4B.72469$9S6.22388@fx02.iad> <BdD4B.56201$bO5.20256@fx16.iad>
From bitrex <bitrex@de.lete.earthlink.net>
Message-ID <eEP4B.94365$AG1.6246@fx42.iad> (permalink)
Organization frugalusenet - www.frugalusenet.com
Date 2017-06-28 11:08 -0400

Show all headers | View raw


On 06/27/2017 09:01 PM, Clifford Heath wrote:
> On 28/06/17 00:07, bitrex wrote:
>> As someone
> 
> Me, I believe.
> 
>> pointed out a while back while the Arduino toolchain seems to
>> be very popular for developing hobbyist embedded applications for
>> AVR/ARM, but the API and IDE is kind of a mess. The API is a bastardized
>> subset of C++ where a lot of things don't work and which most users only
>> use to write kludgey, procedural C-like code; you have C-like global
>> functions such as "digitalWrite" which under the hood use 40 or 50
>> assembly instructions just to write a bit to a GPIO port.
>>
>> I think it's certainly possible to make something better that allows
>> someone who doesn't want to fight with the tools get real work done.
>>
>> I was thinking that since little uPs are really more like programmable
>> hardware rather than general purpose computers, if one's looking to make
>> a microcontroller language for the everyman it doesn't really make sense
>> to derive it directly from a general purpose systems programming
>> language like C/C++.
>>
>> In modern computer games the graphics and physics engines are written in
>> high-performance compiled languages like C/C++. But the whole product
>> doesn't use those languages to define its behavior; usually all the
>> plot, mechanics, and other stuff that make a video game a game is
>> fleshed out in some kind of scripting language at a much higher level of
>> abstraction. This allows people other than rockstar systems programmers
>> to contribute to the design and avoids needing to recompile the whole
>> codebase every time someone decides this enemy spaceship should fire
>> purple lasers instead of green.
>>
>> I think even 8 bit processors with modern architectures are fast enough
>> that one could adapt a similar paradigm to writing embedded apps.
> 
> Yes, I agree. I think a lot of systems would be better implemented
> that way.

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. 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.

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.

>> The
>> high-performance stuff, like interfacing with external hardware through
>> GPIO can be written in C/C++, in something like the "policy-based
>> design" paradigm where you have an abstract type of device, like
>> "Display" or "TemperatureSensor" which defines an interface to the basic
>> functions any device of that type should be able to do, and then
>> "policies" or "plug-ins" which handle the logic requirements of some
>> particular type of TemperatureSensor from some manufacturer. If you want
>> to change the sensor you don't rewrite the entire codebase, you just
>> rewrite the plug-in.
> 
> That's what the creators of Arduino should have done with their C++
> library. C++ is still the right way to build the system you describe.

I think part of the reason the asm generated for many Arduino API calls 
is so bloated is that while it is derived from C++, they made virtually 
no use of the template metaprogramming functionality available in its 
modern incarnations and so there have to be tons of overloads, virtual 
methods, etc. for different targets and user configuration options where 
the compiler has no idea what is needed and what isn't, so has to toss 
in the kitchen sink.

Template metaprogramming is too abstruse for newbs, but ideally all this 
platform-specific and configuration stuff would be templated and handled 
at compile time. The goal of modern C++ is to have "zero overhead 
abstraction" via generic programming; done correctly most of the 
important stuff about the target should be known at compile time such 
that you can certainly use an abstract function like "digitalWrite" in 
your code, but when its compiled the compiler "knows" that it can smoosh 
all that abstract "overhead" code down into a single asm bit flip 
instruction on the proper port for the target device.

>> 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.

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.

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.

> Want help writing the compiler? It's pretty straightforward. Propose
> a sensible syntax (not Basic, and not RPN/Forth!) and I'll look at it.
> It would probably even integrate easily into the Arduino toolchain,
> emitting C++ data definitions to be compiled into flash.
> 
> Clifford Heath.

This project would have to be pretty low on my list of priorities, 
unfortunately, but I'll definitely keep the offer in mind...:-)

Back to sci.electronics.design | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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