125 points · by stevelosh · 9y ago · 70 comments
(define-instruction op-shr (_ r _ _) ;; SHR
(setf (values (register r) flag)
(>>_8 (register r))))
(define-instruction op-shl (_ r _ _) ;; SHL
(setf (values (register r) flag)
(<<_8 (register r))))
Since there is only one reg argument, it seems these are shifting the source register in place. It should put the shifted value of the source register into the target register (8XY6: VX <- VY>>1) This was recently clarified in an exchange on the retrocomputing forum [1].[1] https://groups.yahoo.com/neo/groups/rcacosmac/conversations/...
FWIW:
(setf (values X Y) Z)
is roughly equivalent to the Python: (X, Y) = Z
So (setf (values (register r) flag)
(<<_8 (register r)))
might be idiomatically in python: (registers[r], flag) = leftShift8(registers[r])
And in any event is using the same register "r" as both source and destination.I'm excited to see this blog series develop to the point where there is graphical output.
> If I were to do this in Scheme, I'd use a "parameter" called 'current-chip' (think of it like a dynamically scoped variable) and write getter/setter procedures that operate on the current chip.
And then what, use the flimsy hack known as "fluid let" to locally rebind this parameter to a desired chip instance? Let's avoid lack of macro-expansion-time hygiene, by introducing run-time variable mutation?
Scheme standards before R6RS[0] were a rough consensus on which language features everyone could agree on as useful additions. That's why only syntax-rules made the cut, not because Scheme programmers object to the ability to break hygiene. Implementors disagreed on which mechanism for breaking hygiene was most "correct", so none made the cut and unhygienic macros weren't standardized.
[0] R6RS, in a break from Scheme tradition, adopted a systemd-esque "come up with a standard for these needed features, any standard, and everybody else needs to fall in line whether or not they like it" approach.
I haven't encountered a single case where I didn't prefer hygiene over an unhygienic solution.
One case where you'd want unhygienic capture is with anaphoric macros, which introduce a binding (usually named `it` as in english). For example, using an anaphoric if called aif, we can transform:
(let ((result (big-long-calculation)))
(if result
(foo result)))
into: (aif (big-long-calculation)
(foo it))
cf: http://www.bookshelf.jp/texi/onlisp/onlisp_15.htmlNot all Scheme programmers. Mostly Chicken users, in my experience :p
The rest of us demand hygiene.
> Scheme standards before R6RS[0] were a rough consensus on which language features everyone could agree on as useful additions. That's why only syntax-rules made the cut, not because Scheme programmers object to the ability to break hygiene.
The reason only syntax-rules made the cut is precisely because the only thing the Scheme committee could come to a consensus on, as far as macros are concerned, was the high-level, hygienic syntax-rules system. In fact, the very reason Scheme didn't have a standard macro system for many years was specifically because of the problems of traditional procedural macros being unhygienic.
> [0] R6RS, in a break from Scheme tradition, adopted a systemd-esque "come up with a standard for these needed features, any standard, and everybody else needs to fall in line whether or not they like it" approach.
Err, no. R6RS adopted a 60% vote for the ratification of the standard rather than the 100% consensus approach used previously. This change was largely viewed as a welcome one by the community, because R5RS was delayed for nearly a decade because of one person (R. Kent Dybvig, iirc) holding out on some things everyone else wanted (especially records). Furthermore, the change to a vote was enacted by the Scheme community at large, and the editors of the R6RS were all well-known and respected members of the Scheme community before they assumed editorship. There's nothing systemd-esque nor "fall in line whether you like it or not" about it. The entire process was very democratic.
The main reason R6RS hasn't seen wide adoption has more to do with its size than anything else. However, keep in mind that the Scheme Steering Committee had explicitly tasked the R6RS committee with creating a standard that was both backwards-compatible with IEEE Scheme and made it easier for Scheme implementations to be interoperable. At the time the R6RS process started, Scheme's biggest problem was determined to be the lack of portability available to Scheme programs due to the minimalism of R5RS, which made it inadequate for many real-world programs and lead to each implementation having its own incompatible extensions — as such, programs tended to run on one particular implementation and not any others. This made it especially difficult to write useful libraries for Scheme. While R6RS is far from perfect, the R6RS committee really did an admirable job in achieving that goal, all things considered.
That's true. Although I didn't like the specific decisions they came to myself...
>the problems of traditional procedural macros being unhygienic.
But that wasn't even a problem by the time R5RS rolled around. syntactic closures had been around since 1988, and syntax-case had been around since 1992. R5RS released in 1998. The reason R5RS didn't have hygienic procedural macros was not because such a system hadn't been devised, ir was because the SSC couldn't agree on which one to use.
>Not all Scheme programmers. Mostly Chicken users, in my experience :p
>The rest of us demand hygiene.
That was a regrettable regression from CHICKEN 4, IMHO. I mean, we still have implicit renaming, which is hygienic, but ir is O(n). Frankly, Felix &co should have implemented syntactic closures. Or even syntax-case: as much as I dislike it personally (I agree with Shinn on this one), it's not badly designed, and it would be a heck of a lot better than what we got.
But what can you do? Gambit and Guile got this one right (well, they use syntax-case, but close enough), and debugging (which sucks in CHICKEN, even with Feathers, which is a massive improvement: I can't even evaluate arbitrary expressions on exception. Really?). But despite Andy Wingo's heroic efforts, Guile is still relatively slow, and Gambit has an even worse library selection than Guile's, which isn't that great (although, credit to the Guile devs, the apis on said libraries are really nice).
I could bear the slowness, but the libraries...
Do you mean to create global functions with those generic, unqualified names? The unhygienic solution at least keeps them nicely inside the lexical scope of the "with" construct. That actually looks more hygienic, for another, more traditional, meaning of hygienic.
You mean, nitpicking about trivialities ;-) ?
It is a matter of context. There is a whole blog post full of hard work and you comment about an unhygienic macro. Author said:
This is nice and readable. Some folks will dislike the fact that it introduces new variable bindings that are “hidden” in the macro definition, but I like the concision you get from it, especially for a small project like this.
This is a small project, the code is deliberately using macros in a way that is regarded as poor style (even in CL), but this is not important here. There is no need to start a flame about single namespaces.
http://stackoverflow.com/questions/267862/what-makes-lisp-ma...
Secondly, parameters must be global. You can't, say, have a function that depends on which parameters were bound in a parent function. This is because parameters aren't true dynamic scoping: they're using lexical scoping to emulate dynamic scoping. All well and good, but why not just implement dynamic scoping?
Thirdly, Parameters are fiddly to get right. The path to RnRS parameters is paved with the bones of many a construct (like fluid-let) that tried and failed to safely handle parameters, whether it be because of multithreading, or because of those two famous bugbears of Scheme programming, call/cc and dynamic-wind. The reason for this is that all these constructs, at their heart, are changing the value of a global for the dynamic extent of a function. In scheme, as you no doubt know, dynamic extents aren't guaranteed contiguous.
OTOH, true dynamic variables are a lot simpler to get right in the prescence of continuations: a continuation is a copy of the stack, in low level terms. Dynamic variables are bound to the stack, so dynamic variables and continuations should Just Work. The same is true with threading.
In short, parameters are a construct less flexable than true dynamic variables and a more complicated one to implement, to boot.