IRC channel logs

2023-11-06.log

back to list of logs

<artemist>I'm working on getting mescc running for aarch64 target and my big problem is how to generate instructions with registers that are not nibble aligned. I could use an accumulator register or just output hex from as.scm
<artemist>I'm not sure what people would prefer, neither seems great
<artemist>It may also be possible to modify M1 to make it possible to e.g. or words together, but that sounds annoying
<stikonas>artemist: we already have that in M1 for riscv
<stikonas>and oriansj and I discussed it at some point that it would be nice to convert aarch64 to be word based
<stikonas>so I think people would prefer that
<stikonas>now the problem is how to do it in backwards compatible way if you don't want to rework stage0-posix-aarch64
<stikonas>possibly need an option to specify which mode to use
<stikonas>actually it's hex2, not M1
<stikonas>see e.g. here https://github.com/oriansj/mescc-tools/blob/master/hex2_word.c#L39
<stikonas>though right now that file is very risc-v'ish
<stikonas>word based stuff is far easier to maintain
<stikonas>you basically create a fairly short M1 define list (100 lines or so)
<stikonas>and then you can construct any assembly expression ever...
<stikonas>oh, actually, it's both hex2 and M1
<stikonas>M1 needs some support to be able to output word based immediate constants
<artemist>That wouls be convenient, I'll look at it
<stikonas>artemist: have you seen how mescc deals with riscv64?
<stikonas>i.e. how macros look like
<stikonas> https://gitlab.com/janneke/mes/-/blob/wip-riscv/module/mescc/riscv64/as.scm
<stikonas>because of the way or'ing is implemented, opcode is always the last
<stikonas>but other than that it's quite readable
<artemist>I saw this file though I assumed stuff was nibble aligned like it is in aarch32
<stikonas>nope, riscv is far worse
<stikonas>it's immediates and instructions are not just misaligned
<stikonas>but they are also mangled in complicated ways
<stikonas> https://i.stack.imgur.com/MUKIE.png
<stikonas>e.g. various bits are not in order
<artemist>This feels like something you come up with so your engineering undergrads can feel clever
<stikonas>you can have bit 20, then bits 10 to 1 then bit 11, etc...
<stikonas>it might be that somehow this makes instruction decoding in HW simpler
<stikonas>e.g. registers are always in the same place...
<stikonas>and you can often check sign of immediate earlier
<artemist>Is there a reference for how the or works? I can't see it in the hex2 high level prototype
<stikonas>well, mescc-tools is a C reference...
<stikonas>there is hex2_word.c there
<artemist>Oh, I was looking in stage0-posix, oops
<stikonas>basically for riscv we contstruct 32-bit words as
<stikonas>.01000000 .00020000 .00000300 00000004
<stikonas>which is gets or'ed into
<stikonas>01020304
<stikonas>so everything that starts with . gets merged into the next thing
<artemist>Is stuff swapped again for endianness reasons?
<stikonas>plus for riscv we also redefined % & $...
<stikonas>hmm, I think it is swapped, but not again
<artemist>Oh, sorry, I meant would .01000000 .00020000 .00000300 00000004 turn into 04030201
<artemist>Not sure where I got "again" from
<stikonas>no, I don't think so
<stikonas>the final 01020304 is swapped in memory
<stikonas>but that's the same for x86
<stikonas>anyway, take a look at mescc_tools/hex2_word
<stikonas>probably ignore those % & $... things
<stikonas>and only look at . for now
<stikonas>in risc-v those other thigns implement instruction mangling (those formats I showed you in that picture)
<stikonas>and then the 2nd part is here https://github.com/oriansj/mescc-tools/blob/master/M1-macro.c#L614
<artemist>Thanks! I'll figure out how to port this
<artemist>hm... It's xor not or, that makes sense
<stikonas>oh yes, it's xor...
<stikonas>in practive for risc-v it doesn't matter
<stikonas>as none of the bits should ever be set in more than one operand
<artemist>Mostly just if it's convenient to e.g. default to 64 bit then have a way to switch back to 32 bit, which could work in aarch64
<artemist>(general purpose registers)
<stikonas>aarch64 also has 32-bit words, doesn't it?
<artemist>Yes, I was thinking of whether you use the 64-bit or 32-bit general purpose registers, as a lot of instructions support either
<stikonas>hmm, it's probably different for risc-v
<stikonas>well, risc-v has some of that
<stikonas>e.g. LD, LW, LH and LB loads different sizes...
<stikonas>but generally most opcodes have no concept of 64 vs 32-bit
<stikonas>they just work on all 64-bits
<stikonas>e.g. "addi, reg1, reg2, imm1" works on 64-bit registers in riscv64 but on 32-bit registers in riscv32
<artemist>in aarch64 "add x0, x1, x2" would be the full 64 bits but "add w0, w1, w2" would truncate the result to 32 bits
<artemist>Same registers, it's just "ignore the top half"
<stikonas>yeah, that's different
<stikonas>riscv64 has none of that...
<stikonas>there are some new opcodes though
<stikonas>so there is addiw reg1, reg2, imm1 that works with 32-bit values
<stikonas>but the result is sign extended and writen to 64-bit register
<artemist>that difference might be pretty similar on aarch64, it's just a single bit at the top of the instruction that specifies
<stikonas>hmm, could be
<stikonas>then it's not so different
<stikonas>but assemblers make it less apparent
<artemist>Yeah, assemblers being high level has been annoying
<artemist>aarch64 assembly really doesn't like exposing the existance of pc-relative instructions
<stikonas>well, on riscv it's not a problem
<stikonas>there is lui for absolute loads and auipc for relative loads
<stikonas>well you actually need 2 instructions
<stikonas>lui/addiw or auipc/addiw
<stikonas>but low level stuff (e.g. hex0) is really pain to write for riscv
<stikonas>well, actually only once you reach M0, it becomes nice
<stikonas>even nicer than x86
<artemist>The instructions exist but the assembler really wants you do something like "ldr x0, label"
<artemist>or "mov x0, label
<artemist>mov on aarch64 means so many different actual opcodes
<stikonas>yeah, just like x86...
<stikonas>risc-v is simpler in that regard
<stikonas>mv is actually the same opcode as addi just with zero immediate
<stikonas>but you can't mov x0, label
<stikonas>you have to load it using auipc/addiw
<muurkha>cedb: you will learn Forth eventually if this kind of thing interests you, probably
<muurkha>janneke: congratulations on new stage0 release push!
<muurkha>23:52 < artemist> This feels like something you come up with so your engineering undergrads can
<muurkha> feel clever
<muurkha>that was what I thought about the RISC-V instruction format at first too, but when I went to look at where immediate bits come from, I changed my mind; I think it's actually a significant advantage for tiny, low-gate-count RISC-V cores
<muurkha>I think there's probably a significant class of designs where this gives you a significantly higher maximum clock speed
<muurkha>wrt auipc/addiw or auipc/addi, there's a standard `la` pseudoinstruction that traditional RISC-V assemblers assemble to that instruction pair
<artemist>Yeah, true. I guess if the sign bit is always in the same place it's fewer gates to sign extend
<artemist>Low gate count is an interesting use case I don't think about a ton, especially since I keep seeing bigger and bigger rv64 socs for linux
<muurkha>low gate count is where RISC-V is wildly successful
<muurkha>Linux-capable RISC-V is so far kind of an enthusiast niche
<muurkha>but in the microcontroller market RISC-V is kicking the shit out of Tensilica and in some cases even ARM
<muurkha>also, MIPS. to the point that MIPS the company has totally just thrown in the towel and switched to RISC-V
<muurkha>Xilinx announced yesterday that they're ditching MicroBlaze in the same way
<muurkha>I think likely Loongson or someone will ship a really compelling Linux-capable RISC-V at some point, but so far they're all a bit anemic
<artemist>MIPS seemed on their last legs because of ARM before riscv
<jcowan>risc-v is very close to mips, but is opne source
<jcowan>and the mips company (Wave) has discontinued mips and is using risc-v anyway
<pabs3>and Loongson is moving on to LoongArch, their evolution of MIPS
<pabs3>don't think I have seen them mention any RISC-V stuff
<matrix_bridge><emilytrau> @stikonas artemist fyi if you're hacking on it mescc-tools is currently broken out of the box on aarch64. for the nix packages we apply an extra patch to m2libc https://github.com/NixOS/nixpkgs/blob/85f1ba3e51676fa8cc604a3d863d729026a6b8eb/pkgs/by-name/m2/m2libc/package.nix#L21
<matrix_bridge><Andrius Štikonas> Yeah, I know that pr
<matrix_bridge><Andrius Štikonas> The author hasn't finished it though, somebody should test and merge it
<matrix_bridge><Andrius Štikonas> I think it has 3 changes and mknod was untested