Transcript of RSM Episode 3

RSM project page

00:03 a little quick update again i um uh [Music] in the previous video if you watched that um i was struggling a little bit to
00:12 get um the the sign of assignment here to sort
00:15 of carry over and carry through with the arguments and so the example that i was
00:22 looking at was this branch instruction that takes this
00:27 this negative number here sign number it's also used up here but you know here it's got negative value and we will get a you know we will not get the right value out in the end um anyhow so i worked through that and
00:41 uh one thing that i did was say is wrapped up this little sort of uh help
00:45 function that we made i cannot comment down here
00:51 into a separate function it could be useful just have keep that around
01:02 and uh another change that i made well a couple of changes i've made here first off i fixed up these macros to have a
01:10 prefix and not just be get so eventually and maybe we can do that now
01:15 move this into just a separate file so we can use this for from different source files
01:20 and i also devised an update to this kind of
01:26 instruction encoding naming schema here
01:30 so looking at grab this table it's almost like should have this on the screen it's pretty useful so i'm gonna paste this up here as i'm talking through it's real brief
01:43 okay so so here's a quick refresher right this what the um instruction
01:49 layout looks like it's a fixed 32 bits per instruction the uh the first eight bits is the
01:56 operation code and then the reminding bits are divided
02:00 into arguments so a b c and d argument
02:06 and if d is not needed there's more space for c and c is nothing more space
02:10 for b and so on and when when accessing these uh like accessing c as 14 bits or five bits
02:19 obviously there's going to be a difference there if you're dealing with sign integers or not so the updated uh encoding
02:31 sort of naming schema here is just a letter means that it's a register
02:36 uh origin you know the value in their names a register
02:42 right so here we have a b and that means that that b a and b b
02:46 here um names the the register so like one first
02:53 one and two first two and so on um okay this is unchanged since previous
03:00 videos that means that b contains uh the number like the
03:05 yeah the number of a constant tbt what that means uh and here are the changes uh
03:13 so so bu means that it's an immediate and unsigned integer
03:18 this is what i previously called w for white and that means that in in this example
03:24 here with a b u is that this b value here is the entire
03:31 uh is the entire 19 bits as an unsigned integer in that case you know we could just read this value if you want it would it would still work however
03:42 there is a sibling here called a uh like bs for signed and it means that b is an immediate
03:50 signed integer and now we care about this this bit over here that's kind of and so to
03:59 make the forwarding functions that we have in the in the implementation file
04:03 here still still work nicely these instruction encoding
04:09 little schema thingies here have been updated
04:13 so looking at the two the instructions that i've been looking at or mainly this one let's just look at this one they're the same pretty much um it now it now says here a b s that
04:26 means that b in this case is a signed immediate value
04:32 which for this instruction is a a relative number of instructions to jump backwards or forwards too so it's a number that is added on to
04:40 the uh the program counter when this is a curse
04:46 okay i want to show you
05:03 where i ended up so first off just hashed out these uh max values for the
05:10 wide fields i still put a white up here because at this you know at this granularity so
05:15 to speak there's no this doesn't deal with sinus at all so it's called white
05:19 here but the reminder of the code has been updated too to say either register unsigned or signed
05:27 so ar register au unsigned
05:38 for the new stuff right so what did you stuff the solution to the problem that i was working on before so this is essentially where we left off
05:48 at the end um i used to have been making a little mistake but i found it pretty quickly and clean
05:53 things up um so now and and then i've just sort of
06:00 copy pasted this over here so what uh what what i do here to get the
06:05 uh sign value is first just load it as the unsigned values you know just access the field load bits
06:11 it's the same stuff and then treating the so this is an unsigned number at this point because it's a u here an arg n up here
06:21 it's an unsigned integer okay so then we
06:27 subtract from that half of and then
06:39 reinterpret that as a as a signed integer and similarly there are
06:48 i haven't made these the set operations yet
06:52 because i don't use them but they should be the same as here so
06:57 here we have the equivalent of of setting one of
07:00 these um so let's say we want to set the uh the
07:06 unsigned value of a then we just use this
07:11 make a nothing no qualifier if we want to set a sine integer we use
07:20 the the a s macro here and how this works is essentially the inverse of what what this function up here does
07:31 or function this code does here right so this takes half of the maximum value and subtracts that right and here we take
07:38 half of the maximum value same same okay so that works and
07:47 the rest here is just unchanged i just updated these names here for the new macro names uh these are still the same formula code
07:56 uh this is still completely unchanged and then down here
08:02 i've updated these two to use this new um these two new macros right to make something with a signed immediate value
08:10 so three this is totally unchanged in previous video we fixed a little bug here off by one bug there
08:15 and now when we get down here and call the
08:23 formatting function from the proc let's just run it a few times here
08:28 um we see that we now correctly get negative three out instead of an
08:33 unexpected number as we were getting for so that's cool uh that you know this is
08:38 i think this is your sometimes how it is i don't know how many hours it took
08:46 three hours i don't know i was working through processing the video and stuff like that maybe it took maybe it took me two hours or so until to
08:54 to work through this this tiny seemingly tiny thing but that's just how it is sometimes and that's good
09:02 um but you know now we got that um so let's see what could be a useful thing to do uh it's getting kind of late here in the day and i i think i'm going to just wrap
09:12 it up pretty soon uh and call it a day and keep working on it tomorrow um or maybe tonight if i
09:18 find a fun energy but who knows
09:24 okay so one thing i want to do is to wrap this up that defines the we've been
09:28 looking at this so much and i think this is going to be useful to access and notice those files so to wrap this up
09:33 into a separate file it's got to be a separate header file um so it's not going to be including the format stuff and that's the second step
09:43 we're going to move that to but we're going to start with all of these instruction kind of helpers
09:52 and we'll bring this table with us so let's um okay let's just let's just cut this out
10:03 i'm gonna make a new file we have our n string here
10:18 and our up so this is just this sort of project
10:25 level header file here rsm the h oh it could be nice to like put everything that has to do with the with
10:32 the uh instructions into one place right i think that could be nice
10:35 so so we're going to lift this definition into where do we have it
10:43 oh and then let's see that was not my we want to grab these things too
11:04 um let's keep it until i know if i use it okay
11:20 and actually the ordering here might be might be a little off i prefer to put
11:27 like i prefer to organize my um c header files or or if it's a single c
11:31 file or whatever but with you know includes obviously at the beginning um so yeah we we're actually gonna make this and stuff so steroid
11:43 includes sunday age um that's the project file and then i like to do is to have
11:51 any uh type defs if i can spell
11:55 uh and after that so like if there are structs and stuff right so
12:08 um i prefer to do it this way and then define the the you know in this example
12:12 the little struct like down here so there's a couple of stripes a couple of type depths and i'm not yeah i don't have a strong opinion where i put little macros like what we have here now but
12:24 um probably after the definitions [Applause]
12:34 and let's put this table at the these things
12:45 here where the instructions start and simplify things a little bit
12:51 okay and the instruction is like it feels
12:57 more sensible for that to be at the top like code is just code and there's no magic to it you know like calling things but a consistent name
13:05 makes it so that you know when i come back to this this is fine i'm minus so
13:09 when i come back to this in six months like i'll find my own things you know
13:15 obviously you forget about stuff um and if anyone else would be crazy enough to
13:19 dig through this code it's like also higher likelihood that you'll understand things if like you know wherever it says
13:26 are ins that's the file you go to this is the type you look for um so i just like to keep it kind of so we're gonna go in here so this is
13:39 where we cut things out from now this is you know our build here is
13:43 just gonna go a little bonkers bananas because you know it doesn't
13:48 it doesn't know what's happening anymore so we have to pull that in and say you know there's a um
13:55 there's stuff here that you need to grab are we going to say this is not why it's
14:01 airing out but yeah yeah i know if you're pure purist you'll be like oh you gotta do like if
14:07 not tough and stuff like pragma ones that's supported by the composition i care about and i like it that's good
14:15 and i also have this little thing
14:21 so this really only has an effect if you use a compiler that um has like
14:27 nullability checks and stuff i'm using clang right now and it does so this doesn't affect it's kind of neat um
14:33 now we don't have any function definitions in this header file but
14:37 we're doing this one um and so what this does let's just jump and look at it
14:41 um is that it says assume that pointer types
14:48 are non-null as in if you try to personal value to it that is an expected thing um it has two
14:58 effects so actually let me just sketch this out to explain what it means so let's say we have a function foo here
15:07 and it takes some uh lol cat because what has a cool rate as
15:12 an argument right so now if we cold food here with null like somewhere else right like is this valid
15:27 does is is food prepared to deal with a uh non-pointer right maybe it is maybe it's
15:33 not um and so let's say we have two one and two
15:38 two right and through one here just assumes you know c is
15:42 is a valid pointer and just goes about his business and does stuff and food too here does some some check in its implementation and see if the c is null and maybe it
15:52 just does nothing in that case you know and so the nullability sort of decorators uh allows you to say you know
15:59 no if
16:05 see if if this fu one does not handle a null pointer and for things to do at all pointer you and now when we call
16:15 if you want with the code for one here the compiler can be like no that's not
16:20 that's not gonna work right it's gonna say hey you're trying to you're passing a null pointer to something that doesn't
16:26 handle now and uh and similarly
16:32 uh if we do this the compiler can sort of uh say
16:36 uh that this this is totally okay i guess it wouldn't say that it would just like not complain i guess so this is the this is the example and this the second thing is if you um
16:48 i don't know if if you if you run clang with the undefined uh sanitizer
16:54 which i have enabled in this project uh i'm not sure if if gcc has anything
16:59 like that but i think that's super helpful catch a lot of little little dumb mistakes that make um it can also
17:05 catch these things like nullibility stuff at run time which is kind of neat
17:09 um so in this in this build script which generates the
17:16 uh let's grab my mouse the generates the the the build file like i see if like if
17:21 the if the compiler is clang then enable a couple of flags that enable these in in debug mode um so we have the uh the
17:31 the nullability sense the sanitize you know uh nullability which i i'm pretty sure
17:37 it's part of the undefined sanitizer rather than the address sanitizer
17:42 um since this is more like a memory sanitizer than address sanitizer i suppose but so a couple of couple of flags this kind of kicks that in uh and what's neat
17:52 about that is that at run time even if you do some like cast or whatever right
17:57 if you just like you say you know foo is like um
18:04 let's see you would cast this to a parenthesis right here like
18:13 right like this then the compiler wouldn't be complaining it would just be like oh well you know i guess you can
18:19 you can personal to this because you haven't said anything about it right and even if you do you do nullable right now you just think
18:27 it's totally fine right another runtime it wouldn't be and um
18:31 and then i'll build the features of the undefined sanitizer we'll just cache that for you which is kind of cool i brought that super neat it comes a
18:37 little bit about runtime cost but you know the debug build i care about that
18:43 so that's what this guy does so plug those in here uh just make that
18:48 habit to you know make sure that this header file if it's included multiple times doesn't get you know uh re-populating code
18:57 and uh that any function definitions beyond
19:00 this point and you know before this point which is the end of the file um
19:07 makes it so that if you don't say if you don't say which nullability is for a
19:11 pointer it defaults to that it does not accept null
19:17 which in admirative cases i think is is what functions do and so on and so forth which means that stuff to do the
19:25 deuce except null so like the memory allocator stuff let's see so we have nullable here right so you can use this both for return values and for arguments so for
19:35 example if this function might fail so this function goes to the os and it says
19:39 hey give me you know um some amount of virtual memory um
19:44 and it the os might just say like no or like
19:49 if i failed or like whatever right you get an all back this function however just just initializes the uh the rmm
19:58 structure inside a buffer that usage provided so this can never fail this just returns a pointer and so in this
20:06 case the compiler can insert instrumentation here to make sure but that compile time you
20:11 can do some checks on the runtime then can do some some some analysis here and
20:15 make sure that like this never returns null and if it does it would just like it would you know it would um uh
20:24 abort essentially like trap whatever print little message print little stack trace if you enable that
20:30 and uh and in this case it wouldn't right if this returns null it would like not do that it would be defined
20:37 here as being expected so now let's see what uh what i'm up
20:45 what i'm messing up here okay so this no longer of course makes any sense because
20:49 the uh this this uh type we move this type
20:56 definition so let's move that over so over here so now we do have a function here so i guess that was useful
21:02 this is a function to support this i'm going to put the functions after the
21:07 macros here so we have that of a
21:21 [Music] constant i think we understand what this is don't
21:31 need that okay i'm just going to clear this and rebuild just make sure i can scroll up here so
21:38 unknown type name all right so here we're missing the
21:43 instruction and then oh yeah this so this
21:55 enum definition relies on these macros so we have to put
22:02 those afterwards so let's just do that i'm going to cut those out that too i can't do that use that so i guess there's a compromise and you know you do what you gotta do
22:13 um and we can go back just a little bit and just squish these techniques together [Applause]
22:21 okay and it works so now we've separated that in set of instructions
22:28 into separate headers we can use that in different places so the next thing we can do is to put the formatting code yes into this um
22:37 string formatting file just keep that stuff separate
22:42 um and then we can you know in the in the next video we'll start with a virtual machine to evaluate this program
22:50 here and then we can use the space here and there in our main in our main program it's sort of like a
22:56 this is kind of the working area where we're we're building things out and as
23:01 things are kind of stabilizing and kind of working we can move them out into a separate source file i think it's very easy to um
23:10 it probably doesn't seem like it i probably seem like a total noob on this stuff but you know i've been programming for decades at this point several decades
23:19 and um you know i'm still learning a lot uh
23:23 just doing this i think you've you've seen me learn some stuff but uh
23:27 but i've also learned some stuff that i've been able to um to memorize
23:33 uh or or truly learn from i think um and one of them is to
23:41 try to not optimize like anything too early
23:45 and i'm not talking about like performance here necessarily although that's maybe obvious but you're sort of like structure for example right if we go over here
23:55 uh i see my my like camera is like struggling a little bit is it because there's like a sunspot
24:02 there or something i don't know well i guess you're you're
24:08 not looking at my face too much anyways maybe i can is this a manual focus
24:17 no no it doesn't it doesn't care about what i'm asking it to do um well okay well we'll leave the camera as it
24:40 is okay so so back to one of the things
24:44 that i think is kind of important or at least very useful when it comes to organization is um just keep things like very you know if you look at you just do this
24:58 you can see all the files right um this and the source director here has
25:03 like fairly few files the files that are here are files that are contained stuff that is kind of done
25:09 and i think this is kind of my point that i'm doing a poor job of explaining
25:14 you start out like um just
25:18 kind of in a kind of a in a workspace almost like in
25:22 a single file just like do make come up with stuff like make it
25:28 work uh find the groove you know find sort of like the the shape of the the
25:32 types and the functions and arguments and stuff like that and start using them
25:35 and after a couple of times you've used them and you haven't needed to change them like at that point i think that's when they can like move into their own little unit
25:45 um like a separate source file and and also coincidentally that's usually when they have some at least that when i have some sense of
25:53 what the name for it should be you know in this case like yeah format kind of works you know i just try that for a while it kind of works for this stuff this is a better example
26:02 like started out by us calling this like r
26:07 off and orange and uh you know there were a couple of different um different
26:12 names going around and at this point with with uh uh with the with the time
26:17 spent on this i think it's pretty obvious that this should just be called like instructions right or our ins whatever
26:26 so uh start you know starting out by like
26:31 trying to be like hmm let's like whiteboard this and see we gonna need like this module and that thing and like
26:37 this source file and you make subdirectories so you make a bunch of
26:42 header files ahead of time for things that you will need and try to structure
26:45 things up ahead which i've done like so many times and it never really works out at least it is incredibly
26:52 inefficient and i always end up moving things back and forth and depending on the programming language you use there's higher or lower cost to it right if you use go as a very low cost for doing that which i think is is a really neat property of go if you use
27:05 something like c as in this case the cost is like can be somewhat high
27:09 um especially if you use a lot of header files right you might have to go and rewire a lot of includes and stuff like that if you use something like javascript like every file is gonna have
27:18 to change it imports and exports and stuff or imports at least
27:23 anyhow it's a little fun a little fun thing so i guess we've we've seen me
27:27 doing that here a little bit where we've just been working out of this main file here that contains you know the main function and we've just been spending like trainings on and messing with things and then wrapping them up into functions like this logged in function and these
27:42 markers here and when that works we're now moving it into its own little
27:49 source file so let's uh let's do this with the formatting code as well
27:54 so let's move that into um into this file i'm going to keep this at the bottom these are generated code anyways
27:58 right by the by the macros here so probably
28:04 use that at the top also this way since this is just an internal like an implementation file right if we
28:15 were ever to want to use this rsm for a different project you know in a library style or something like that um we we want to keep the header file like
28:25 small and we want it to be name spaced um and there's a balance there between
28:29 you know if you if we prefix all of these right with um with a namespace then we're taking that cost in like everywhere right including
28:38 here but this doesn't need to be part of any sort of you know um top level api so we just toss them in here use a short name it's very nice
28:47 um we're gonna have to we're gonna have to declare this in a header file somewhere so i'm gonna take that so we're gonna save this save this and let me
28:59 run this this build script again so yeah now we can't find that right so we're calling that here and it's like i don't know this is i'm not gonna continue um and just add
29:16 this uh function prototype and explain what it does
29:21 uh formats uh a an array
29:28 of instructions ip um some play
29:38 text to buff all right and then since we're
29:43 using this r8 buff that i've talked about before i actually have since i've
29:47 used this so many times i've made a little template that i can use with
29:51 functions that just it's going to be dropped in here
29:56 it's a double competent so it writes the most buff cap blah blah blah this is just essentially
30:02 what we talked in the previous video i was talking a little bit about
30:08 uh sdn print def from libercy so this just it has the same paper as a printf essentially okay
30:15 save that it will now find it but now it will be angry but because this
30:22 oh gosh oh this is tricky so now this right our ins is a type defined in here
30:26 an orange so i think what i have to do actually is
30:32 to uh to undo this move to move rn stripe def into this header file and instead move
30:37 this back here um let's move that to the top
30:49 and do c [Music] martins.h for details so to future me who will be confused
31:02 okay now we have that and we can also our up
31:09 is down here since we have someone typed up why not okay
31:19 generally the closer related information is the better
31:25 and that goes for things like comments and what they're about to great big
31:30 things like unit tests and the coded tests i think is the the closer things
31:34 the closer you can get these things together is the the better chance of them like
31:39 working out when it comes to other people and your future shelf it is so
31:43 great rule like as short distance as possible between related things
31:50 okay so now we kind of uh separated that so if we if and when we need to expand
31:55 on our formatter we can just do that in here it's a nice little self-contained
31:58 thing um as we're expanding instructions uh we
32:05 can just do that in this file and it'll be easy to find but like you know sort
32:10 of a type of thing and our working area here has been
32:14 cleaned up a little bit and logged in this is kind of a helper function let's move this
32:20 uh yeah let's just move this into format
32:24 for now this is probably not the right place but um you know this is one of the nice things with c i suppose we can just kind
32:31 of put this in any implementation file and the link here we just figured out for us and since we're only using it in this file
32:41 is put the prototype um c fmt let's see
32:45 okay actually my editor can't find that for
32:50 me so i can just do that and i can find it for me so i'll just understand that
32:56 okay i think we're gonna stop there um we've cleaned things up a bit
33:02 uh earlier we solved this issue with the assigned immediate values
33:11 we've looked at some uh ways by which we could or i talked
33:16 about at least in some ways we can um start by
33:19 um exploring stuff in kind of a workspace and start with a single file
33:26 as things gradually expand then put them into functions like lift the code put it
33:31 into a function or a macro whatever and as the function stabilizes you use that a couple of times right still in your working space move that function into its own dedicated or set of functions into its own dedicated source file um and that's kind of like one two three
33:44 is kind of step step process like that um
33:50 reduces a lot of thrash from trying to just like figure out good names before you even know like what
33:56 good names are and what the groupings are um cool yeah and tomorrow i'm gonna
34:03 put together a little uh virtual machine to actually execute this give it some inputs see what the outputs are and that's gonna be fun so
34:09 catch you later