I’m currently working on a bit of a long-winded project to recapture
QuickBASIC, the environment I learned to program in, in a bit of
a novel way, while remaining highly faithful to the original. (I actually
started a related project 9 years ago (!), and I’ll be able to reuse
some of what I did back then!)
The new project involves compiling the BASIC down into a bytecode suitable for
a stack machine, and an implementation of such a stack machine. Importantly,
there’ll soon be a second implementation which runs on a different architecture
entirely: namely, it’ll be an implementation of this architecture on an FPGA.
So the machine needs to be reasonably easy to implement in hardware — if it
simplifies the gateware design, I’ll happily take tradeoffs that involve making the
compiler more complicated, or the ISA more verbose.
For the binary operations, the stack machine would pop off two elements, and then
look at the types of those elements to determine how to add them. This is easy to
do in Zig, but this is giving our core a lot of work to do — especially when we
consider how extensive the rules involved are:
Ideally, the executing machine doesn’t ever need to check the type of a value
on the stack before doing something with it — the bytecode itself can describe
whatever needs to happen instead.
PUSH_IMM_INTEGER(42)LET(0)PUSH_IMM_LONG(32800)LET(1)PUSH_VARIABLE(1)PUSH_VARIABLE(0)OPERATOR_SUBTRACT// runtime determines it must promote RHS and do LONG subtractionCAST_INTEGER// runtime determines it has a LONG to demoteLET(2)
— and so leaving the runtime environment to decide when to promote, or demote,
or coerce, and when to worry about under/overflow — we instead have this:
PUSH_IMM_INTEGER(42)LET(0)PUSH_IMM_LONG(32800)LET(1)PUSH_VARIABLE(1)PUSH_VARIABLE(0)PROMOTE_INTEGER_LONG// compiler knows we're about to do LONG subtractionOPERATOR_SUBTRACT_LONGCOERCE_LONG_INTEGER// compiler knows we're assigning to an INTEGERLET(2)
A proliferation of opcodes results, but crucially it means most operations
execute unconditionally, resulting in a far simpler design when it comes to
putting this on a chip.
The upshot of this design, however, is that the compiler needs a far greater degree
of awareness and ability:
It needs to be aware of what types the arguments to the binary operation are.
In the example above, we have a LONG and an INTEGER. This appears trivial, but
we need to keep in mind that the arguments can be arbitrary expressions.
We need this in order to determine the opcode that gets emitted for the
operation, and to know if any operand reconciliation is necessary.
It needs to be able to reconcile different types of operands, where necessary.
BASIC’s rules here are simple: we coerce the lesser-precision
value to the greater precision. In effect the rule is
INTEGER < LONG < SINGLE < DOUBLE.
STRING is not compatible with any other type — there’s no "x" * 3 = "xxx" here.
It needs to be aware of what type a binary operation’s result will be in.
I started out with the simple rule that the result will be of the same type
of the (reconciled) operands. This worked fine when I just had addition,
subtraction and multiplication; above, we add a LONG and an INTEGER, the INTEGER
gets promoted to a LONG, and the result is a LONG.
Division broke this assumption, and resulted in the motivation for this
write-up.
It needs to be able to pass that information up the compilation stack.
Having calculated the result is a LONG, we need to return that information to
the procedure that compiled this expression, as it may make decisions based
on what’s left on the stack after evaluating it — such as in the first
dot-point here, or when assigning to a variable (which may be of any type, and
so require a specific coercion).
This all kind of Just Worked, right up until I implemented division. I
discovered QuickBASIC actually has floating and integer division! Young me
was not aware of the backslash “\” operator (or any need for it, I suppose).
Floating division always returns a floating-point number, even when the inputs
are integral. Integer division always returns an integral, even when the inputs
are floating. The precision of the types returned in turn depends on that of the
input types.
operands
fdiv “/”
idiv “\”
INTEGER
SINGLE
INTEGER
LONG
DOUBLE
LONG
SINGLE
SINGLE
INTEGER
DOUBLE
DOUBLE
LONG
Output types of each division operation given (reconciled) input type.
This presented a problem for the existing compiler, which assumed the result
would be the same type of the operands: having divided two INTEGERs, for
example, the bytecode emitted assumed there would be an INTEGER left on the
stack, and so further emitted code would carry that assumption.
Upon realising how division was supposed to work, the first place I made the
change was in the actual stack machine: correct the behaviour by performing
floating division when floating division was asked for, regardless of the
operand type. Thus dividing two INTEGERs pushed a SINGLE. The (Zig) machine then
promptly asserted upon hitting any operation on that value at all, expecting an
INTEGER there instead. (The future gateware implementation would probably not
assert, and instead produce confusing garbage.)
And so opened up a new kind of bug to look out for: a mismatch between (a)
the assumptions made by the compiler about the effects on the stack of the
operations it was emitting, and (b) the actual effects on the stack produced by
those operations running on the stack machine.
Rather than just some isolated tests (though short of creating some whole
contract interface between compiler and machine — maybe later), why not be
thorough while using the best witness for behaviour there is? Namely, the
compiler and stack machine themselves:
test"compiler and stack machine agree on binop expression types"{for(std.meta.tags(Expr.Op))|op|{for(std.meta.tags(ty.Type))|tyLhs|{for(std.meta.tags(ty.Type))|tyRhs|{varc=tryCompiler.init(testing.allocator,null);deferc.deinit();constcompilerTy=c.compileExpr(.{.binop=.{.lhs=&Expr.init(Expr.Payload.oneImm(tyLhs),.{}),.op=loc.WithRange(Expr.Op).init(op,.{}),.rhs=&Expr.init(Expr.Payload.oneImm(tyRhs),.{}),}})catch|err|switch(err){Error.TypeMismatch=>continue,// keelatud eineelse=>returnerr,};varm=stack.Machine(stack.TestEffects).init(testing.allocator,trystack.TestEffects.init(),null,);deferm.deinit();constcode=tryc.buf.toOwnedSlice(testing.allocator);defertesting.allocator.free(code);trym.run(code);trytesting.expectEqual(1,m.stack.items.len);trytesting.expectEqual(m.stack.items[0].type(),compilerTy);}}}}
We go as follows:
For all binary operations, enumerate all permutations of left- and right-hand
side types.
For each such triple, try compiling the binary operation with the “one”-value2
of each type as its operands. (For integrals, it’s literally the number one.)
If we get a type error from this attempt, skip it — we don’t care that we can’t
divide a DOUBLE by a STRING, or whatever.
If not, the compileExpr method returns to us the type of what it believes
the result to be. This is the same information used elsewhere in the
compiler to guide opcode and coercion decisions.
Create a stack machine, and run the compiled code.
Seeing as we didn’t actually compile a full statement, we expect the result
of the operation to be left sitting on the stack. (Normally a statement
would ultimately consume what’s been pushed.)
Assert that the type of the runtime value left on the stack is the same as
what the compiler expects!
I love how much this solution takes care of itself. While it lacks the
self-descriptive power of a more coupled approach to designing the compiler and
runtime, it lets the implementations remain quite clear and straightforward to
read. It also lets them stand alone, which is handy when a second implementation
of the runtime part is forthcoming, and is (by necessity) in a completely
different language environment.
There was greater value than just from floats, of course: relational operators
like equal “=”, not equal “<>”, etc. all return INTEGERs 0 or -1, but
can operate with any combination of numeric types, or with pairs of STRINGs.
Bitwise operators like AND, OR, XOR, IMP (!), etc. can operate with any
combination of numeric types, but coerce any floats to integrals before doing
so. I bet there’ll be more to uncover, too.
Hope this was fun to read!
I will get some real BASIC highlighting up here by the next
post on this! ↩
I first used each type’s zero value, but then we get division by zero errors while trying
to execute the code! ↩
Reviewing all the medications I’ve ever been on. This will be intensely specific to my experiences;
YMMV! Content note: mentions of mental illness, suicidal feelings, assault, sex.
mental health(-ish)
escitalopram
A day or two after starting it I felt like the world had colour in it again for the first time.
Not very effective for anything other than “light depression” (?) imo.
Prescribed initially for depression/I’m 23 And Life Is Generally Hard, was on it for two years.
Later prescribed in an attempt to counter chronic idiopathic nausea, to no effect.
quetiapine
Wow! I didn’t know what “suicidal compulsion” actually felt like until I took my first dose.
That was also my last dose.
desvenlafaxine
Going up from 100mg to 200mg, I’d just stop knowing how to form sentences halfway through them.
Effective on moderate GAD-type symptoms.
Coming off it was, contrary to all expectations, pretty uneventful. Except that for about three
weeks, all I could smell was blood, with no apparent source.
brexpiprazole
If you close your eyes, you will fall asleep. At any time.
Definitely calm.
Much too sleepy to continue, but it definitely brought me down after increasingly intensifying
distress post-sexual assault.
As with any antipsychotic/dopamine antagonist, not particularly inclined to remain on one
for any longer period of time.
At the time I was put on it, the only article in the literature about it had the (actually!)
reassuring title: “Brexpiprazole: so far so good”.
lamotrigine
Unclear whether I felt much of anything on this, even after three years at 200mg.
Coming off it: ANGER. Wow. (Remember to titrate; SJS can be triggered both on increase and
decrease, apparently.)
After coming off it: oh my goodness I suddenly feel like being creative again.
mirtazapine
eeeeeeeepy and hunnnnggggy.
Extremely and rapidly effective with chronic idiopathic nausea. Nothing else worked.
Weight gain pretty woof. That said, the weight I gained on it, I managed to drop while still on
it.
I don’t remember if I maxed out at 30mg or 45mg — at higher doses the dissociation made
memory very wonky.
Discontinuation meant a lot of rebound nausea and anxiety. It took me about 4 attempts, over
several years, with various titration schedules; the successful attempt was just dropping from
a sustained 15mg to 0. Trying to step it down at 7.5mg only prolonged the unpleasantness and
resulted in aborting the attempt — subtherapeutic but side-effects remain.
duloxetine
Really pleasant and noticeable, even at 30mg. Everything feels a bit lighter, and mild
neuropathy totally neutralised.
Night sweats :( Became intolerable not long after increasing to 60mg, and didn’t subside after
reducing again.
Discontinuation was reasonably uneventful; (manageably) shorterer temper and very mild nausea.
gabapentin
I just started this! Early effects for neuropathy are promising. Slightly dissociative/floaty.
Was always curious about gabapentin/pregabalin since I first started having noticeable anxiety,
so I’m glad I’ve gotten to try it. Definitely somewhat calming.
diazepam
It is what it is.
Avoid taking on an empty stomach if you’re having issues with nausea — it can get so much
worse.
I treat it with extreme caution. I’ve read so many personal horror stories about benzodiazepine
dependence and it takes a really bad time for me to consider taking even 2.5mg. I’ll go up to
5mg if need be, but in general would refuse to take it if I’ve had any separate dose within the
last week.
For the above reason, despite having been in some very bad places (where doctors were
prescribing it in larger quantities at a time), I’ve maybe taken 100mg total over the last
decade. Just not worth the risk.
Even less inclined to if I’ve taken stimulants on the same day, but I’ve only ever once
(namely, in a life-threatening situation) had to even consider diazepam since getting medicated
for ADHD 2 years ago. Funny how that works!
temazepam
Not a fan; taken a handful of times ever, mostly at music festivals.
I have chronic insomnia and this is just not any part of an answer to it.
ADHD
dextroamphetamine
Current weapon of choice. Calm, alert, focussed.
Latent anxiety is reduced a lot, probably because it lets me have some agency in my own head.
My doctor gradually increased my dose to 10mg, 4 times a day. This is absolutely fucking nuts.
Don’t do that.
After 1.5 years I’ve settled on 2.5mg, twice a day at a 4 hour interval, and it’s completely
adequate.
If I’m having a very long day, a third dose is fine too. 2.5mg doses are very light.
For a long while there I was finding myself increasing my dose by 2.5mg or 5mg (per day) every
week or so since the tolerance builds so rapidly. You will deplete every last monoamine you
have like this. I managed to tolerate 40mg/day (after a buildup) for about two days before I
crashed. Do Not.
Sometimes 2.5mg doses can feel a little bit too light, but my health right now is such that
increasing even just one of the doses leaves me feeling empty the next day. I’m glad to have
something that works.
Small doses is also good because I’ve yet to see a psychiatrist in Estonia to get a
prescription here, so I’m working through my stock from Australia very slowly. As far as I can
tell, currently authorised dextroamphetamine products haven’t been imported to Estonia,
so it’s unclear how easy it’d be to access it.
There’s this one, but it’s only available in 10mg? And searching for it online shows up
something with the same name, but it’s atomoxetine instead? Very suss on that.
lisdexamfetamine
Hrrrrrrr. Not a fan.
Something about this leaves me feeling depleted at any effective dose. I think I like the
little humps I get between dex doses — lisdex is just exhausting, and an absolute pain if you
wake up late.
30mg? Not enough to feel anything — too spread out.
40mg? I kinda feel something. It’s a bit eh.
50mg? Yeah, okay, maybe this starts to feel like I’m having an OK dose of dex — I can think,
focus, actually listen to people when they talk to me. I feel like I am spread very thin.
60–70mg? Effective — wired — but also I start sleeping longer and longer because of how
exhausted it leaves me, making my doses later and later, in turn fucking up my sleep schedule;
not to mention the pervasive sense of Bad that starts to accumulate. No ty.
My god. I tried combining ~low dose lisdex with a small dex booster in the afternoon. Quick way
to know what it feels like to fry your brain. You can do all the stimulant equivalence
arithmetic you want, it just doesn’t add up that way.
If I was offered lisdex or nothing, it’s not clear to me that I would take it.
In general, I’m not sold on long-acting stimulants. I get the abuse angle (it’s what I was put
on first, for this reason), and I do read stories of folks who find it legitimately helpful —
like most others with ADHD, I regularly forget to take my stimulants — but there was no way
to make it work for me. It’s too inflexible, and there’s some (possibly fear-mongering) news
out there that suggests long-acting stimulants carry a greater risk of psychosis than
short-acting ones?
methylphenidate
Very conflicted on this. (Annoying that it’s so much more readily available, especially in
Europe.)
Possibly neuroprotective vs. an active COVID infection! I was on MPH when I finally
got COVID, and was wondering if I should keep taking stimulants through a moderately bad case
of it or not. This article swayed me in favour of maintaining it:
Remarkably, several drugs acting on receptors to neurotransmitters also appear to decrease
hospitalization risk: rizatriptan (OR=0.118, CI 0.003 to 0.750), bupropion (OR=0.399, CI
0.136 to 0.976), and methylphenidate (OR=0.444, CI 0.178 to 0.972).
I’ll take an OR of 0.444ish. I had to argue with a doctor from the government(!) to avoid
hospitalisation, but avoid it I did.
I haven’t gotten a sense for how shared this is, but I get noticeable euphoria when starting on
10mg, and it makes one very inclined to up the dose when you stop getting that response after a
few days.
Dragon-chasing aside, MPH consistently produces a sort of global “bad” sensation that I just
cannot with. Even with the initial euphoria, it’s still noticeable — there’s just this
unpleasant but persistent background qualia, almost a certain flavour of experience. Unclear if
shared or just me.
All that notwithstanding, from 10mg it feels 70–80% as effective as dex at ~equivalent dose
(5mg dex ≈ 10mg MPH).
5mg didn’t feel like it had any impact at all, but that was a while ago, and I’d revisit it
if I had to.
If I was offered MPH or nothing, I would probably take it.
If I was offered MPH or lisdex, I would probably take MPH.
vascular
propranolol
Very chill. Noticeable sensation in the chest.
I once had a kind of shock reaction to a piercing which resulted in a really high heart rate,
intense and sudden nausea, cold shivers all over, and me on the floor. (I find lying on a cold
hard floor fixes most things.) The next time I went for a piercing I took 10mg propranolol
before and it was easy.
Initially prescribed to try to combat panic disorder/idiopathic nausea. It at least brought my
heart rate down and made it possible to eat again.
Later took it for arrythmia on demand. Reasonable at this — would often get painful(!)
arrythmia at night which would make sleep impossible, and leave me feeling sore (and exhausted)
the whole next day. 10mg propranolol would usually convert it within half an hour.
100–200mg magnesium seems to work for that too.
nifedipine
Certainly feel that one on starting. Not like I didn’t already have some orthostatic
hypotension …
Take for Raynaud’s in the Australian winter/unspecified (micro?)vascular issues. Recently my
legs have been feeling Not OK and so I’ve restarted it. Unfortunately, we don’t seem to have
the modified-release ones here, so I have to dose twice a day.
Doesn’t impair exercise.
Not to be combined with propranolol (or magnesium, possibly).
gastric
esomeprazole/omeprazole
Unremarkable. Not super effective for (probably stress-induced) heartburn/reflux.
Was also prescribed it at one point as an attempt to help with idiopathic nausea — no
response.
ondansetron
This is meant to be the Big Guns when it comes to nausea, and it did nothing for mine.
I was scrambling to find something that would work — it’d slowly subsided on desvenlafaxine
in the first instance, and I’d tapered off that after about a year and was all good. A
particularly bad anxiety attack provoked it into a full reoccurrence, which was terrifying. I
wasn’t inclined to retry stepping up on desvenlafaxine and waiting it out to see if it’d work
again — I felt like it might’ve also just been a matter of time, then, as it remitted only
after more than a year of (essentially constant) suffering! — so I looked for other things.
My research lead me to mirtazapine; there’s a number of different ways in which it’s
antiemetic. My GP at the time was happy to prescribe it for me based on my conviction,
but wanted to try ondansetron first to see if it’d work as a circuit breaker (and thus avoid
necessitating something taken over a longer time). Alas.
cannabidiol
Usual effects of cannabis products apply.
THC oil
First trials found it to be extremely intense and disorienting.
Retrying years later (once I was using flower more medicinally and regularly), found it to be a
nice way to dose THC in a more controlled manner, without the usual associations that come with
vaping bud (i.e. “Time To Relax, And Perhaps Eat Much Šokolaadi.”)
Slower onset can be quite pleasant.
CBD oil
First trials found it to make me feel generally unwell!
Later trials together with THC oil found it to be a nice way to dampen some aspects of the THC
high, if needed.
cannabis flower
Nothing much to report here. Australian medicinal cannabis was decent, actually decent value
(per volume), and there was a reasonable variety of strains to choose from, which improved over
the years. Doctors were not inclined to gatekeep (perhaps concerningly so) or judge.
Driving was a bit nerve-wracking; regular use builds up a level in the blood which can be
detected by roadside random breath tests even if you’ve not used any that day, so I was pretty
much always on alert/police-avoidance mode. It was a pretty strong deterrent, but I lived in a
public transport deadzone so there wasn’t much choice.
Mild nausea during the first two weeks of starting it, otherwise side-effect-free.
Been on and off it a half-dozen times over the last 8 years as my risk factors have increased
or decreased. I have nothing but good things to say about PrEP.
Caution while travelling; less progressive governments may infer any number of things about you
based on the medication you carry.
HRT
oestradiol valerate
Standard first-line transfem primary HRT.
In the first few years I seemed to manage decent levels while increasing to 8mg/day, but over
time I seemed to stop being able to absorb oral oestrogen and had to switch off.
Not particularly sad about that: other forms are less work for my liver.
oestradiol gel
Absorbs well. A little bit annoying to make your skin sticky twice a day but wycd ¯\_(ツ)_/¯
Seems to be widely available; the last few years have had many HRT shortages, but I’ve not had
issues with this.
micronised progesterone
Preferred over synthetics (e.g. levonorgestrel) due to much reduced blood clotting risk, fewer
negative mood side-effects.
Helpful for sleep — take at night.
First time I started it, within days I was sobbing (+) while watching Frozen. Really unlocks
some emotions.
200-300mg is an actual recreational high (N.B.! loss of consciousness/respiratory depression
may result at extreme doses).
Also an antiandrogen!
spironolactone
At least in Australia, first-line antiandrogen.
Diuretic and generally not fun. At the doses required for me, also precipitated pretty intense
low mood which abated upon discontinuation.
cyproterone
Antiandrogen. Extremely effective in even tiny doses.
My doctor started me on 50mg a day. I don’t have a good analogy for how nuclear this is.
May need more initially to get levels down, but (depending on prevailing E levels) you may be
fine with 12.5 mg once a week, i.e. 3.6% of the dose I was started on.
Some folks report low mood on cypro; it hasn’t been an issue for me.
After the better part of 20 years working with Python, it still managed to
surprise me today.
I’m so used to languages treating x += y et al. as pure sugar for x = x + y
that it skipped my mind that some don’t.
I’m not surprised that you can override them separately in some languages (e.g.
I simply assume this to be the case in C++, and on checking it turns out to
be true — but that seems fair enough given the scope of the language), but I
really am so accustomed to them being only sugar in Ruby that I assumed the same
would hold, at least in effect, in Python.
Thus my surprise on some_list += x modifying some_list in place (unlike
some_list = some_list + x), but once observed, I realised there’d be a
separately-overridden operator function — namely __iadd__ — and so I
figured it “had” to be that way.
Or did it? I then found myself assuming it’s because these operators can’t
actually reassign the receiver, but in fact they can and do: the return value is
what’s assigned to the LHS. So it’s just a matter of convention.
I’ve been getting back into using CXXRTL and Zig together, so I’ve extracted
and rendered somewhat reusable the bindings I made to use them together!
zxxrtl uses CXXRTL’s C API to provide a somewhat idiomatic way to access,
manipulate, and respond to events happening in the design. Its README covers the
setup — it’s a bit involved as it’s necessarily something of a build system,
but once you’re done it’s good to go and flexible enough to be instrumented from
a higher build system.
I’m going to paste the example usage here; this doesn’t use the Sample API for
edge detection, and just drives the simulation while optionally recording VCD:
constCxxrtl=@import("zxxrtl");// Initialise the design.constcxxrtl=Cxxrtl.init();// Optionally start recording VCD. Assume `vcd_out` is `?[]const u8` representing an// optional output filename.varvcd:?Cxxrtl.Vcd=null;if(vcd_out!=null)vcd=Cxxrtl.Vcd.init(cxxrtl);defer{if(vcd)|*vcdh|vcdh.deinit();cxxrtl.deinit();}// Get handles to the clock and reset lines.constclk=cxxrtl.get(bool,"clk");constrst=cxxrtl.get(bool,"rst");// These are of type `Cxxrtl.Object(bool)`.// Reset for a tick.rst.next(true);clk.next(false);cxxrtl.step();if(vcd)|*vcdh|vcdh.sample();clk.next(true);cxxrtl.step();if(vcd)|*vcdh|vcdh.sample();rst.next(false);// Play out 10 cycles.for(0..10)|_|{clk.next(false);cxxrtl.step();if(vcd)|*vcdh|vcdh.sample();clk.next(true);cxxrtl.step();if(vcd)|*vcdh|vcdh.sample();}if(vcd)|*vcdh|{// Assume `alloc` exists.constbuffer=tryvcdh.read(alloc);deferalloc.free(buffer);varfile=trystd.fs.cwd().createFile(vcd_out.?,.{});deferfile.close();tryfile.writeAll(buffer);}