Hello and welcome to part three of implementing the bodies.
In this lecture we are going to finish implementing the paths of finalized body function.
The very first thing I want you to do is scroll up and parse the dot, see all the way to the top and
just above struct history.
We're going to make a flag, so we're going to create an enumeration and the flag is going to be history
flag inside uniform and we're going to set that to 001.
So there should be seven zeros and a one.
So one, two, three, four, five, six, seven and a one.
Perfect.
I always write them in binary because it's just easier, because then you can just put the one to the
left.
You know what I mean?
As you go down here, you can just do things like that and this becomes the one and that's a zero,
right?
And it's just easier to read, in my opinion, which is why I like to write it in binary.
Feel free to use hexadecimal if you want.
So now that we've created that enumeration bit masks, let's scroll all the way down back to finalize
body.
And we're just going to say if history flags ampersand history flag inside union.
So essentially if this is true it means that the body we're finalizing is a is a body of a union, essentially.
So someone's created a union and we're inside the body of that union.
Now, if that's true, then we need to obey the rules of unions.
And what is the role of union?
While a union size is the largest possible variable inside a union, that's the size of a union.
Right.
Because, as you know, unions share all the memory inside.
So if we have a union like this and we go in it, be an it a, then the union size would be for bytes,
right?
And B and A both point to the same memory address.
So what we're going to do here, we're going to obey that rule.
And that's why we're checking if we're inside a union.
So if we are inside a union, we're going to say if the largest possible via node is not.
No.
In other words, was there a variable inside this union body then?
Asterisk variable size equals variable size largest possible var node.
Okay.
So what we're saying here is if if there is a largest possible variable node, which there will be,
if this union has any variables at all, then set the variable size to that largest variable node size.
That's what's going on here.
And we can see it's complaining about calling this function.
Let's just rename variable size to underscore all variable size, right?
Because that's why we're having that problem there.
So just to underscore variable size and now just to underscore variable size like that.
So that that should be straightforward, right?
So if it's a union, then we grab the largest variable node inside that union.
In the example I just showed, that would have been an integer.
And then we set the size to it because unions are not the sum of all their contents, they are literally
the size of the largest element inside them.
And that's what we are checking for here and asserting, okay, so we're not done further down here.
We're going to say in it padding equals compute some padding body vec.
Okay, so remember what I said before.
This will essentially go through all the variables and add all the padding together.
Some of the padding.
Okay.
And then we're just going to say asterisk, variable size plus equals padding.
And now we're just going to say if largest align eligible var nodes is not null, then asterisk underscore
variable size equals a line value as equals.
And so variable size largest align eligible var node var type size.
So if we have a largest align edge with var no provide it to us, then we need to align our variable
size to that align eligible var node.
And now we're going to say ball padded equals padding does not equal zero.
And then we're just going to say our bodies are padded equals padded and we're going to change this
variable size to underscore variable size like that.
Okay.
So that is our finalized body function.
I'm now going to go through it and explain it to you again so it makes a bit more sense.
Okay.
So the first thing we do is we say, are we inside a union?
That's what we do and we check the history flag.
Is this a union body we are finalizing?
If that's the case, then we check to see if there's a largest possible wear node.
Now this will not be null so long as the union has any variables in it at all.
And if it's not null, then it will be the largest variable node that could be found.
So variable node has if you have a union with a char and an integer to two variables.
China's largest possible value will be equal to the initial variable node because it has the largest
size.
So that's what that's about.
So here we say ask is variable size equals variable size, largest possible var node.
So what we're saying is set the variable size of this body to be equal to the variable size of the large
possible var node.
And the reason for that is, is as I've said, unions, they share the memory.
So union size is the largest variable node.
OC is the largest variable inside the union and we can see the variable size that just returns the date
data type size essentially of the variable node provided.
The next thing we do is we compute padding because structures and unions require padding.
So that will go through the body vector and it will sum all of the variable nodes padding together.
That's what that does.
Next we say variable size plus equals padding and that then makes it aligned.
Okay.
However, this is the bit you might be confused about.
What do I mean by our largest saline eligible var node?
Well, we are only interested in aligning to primitive variable sizes.
So if there was a structure variable, it would it could potentially mis align mis align our structure.
Right.
So this large saline edge above our node will be equal to the largest primitive variable.
It will ignore the structure variables.
And that's why we have this, this difference.
So that is why we have largest possible var node and we have a large saline eligible var node because
the structures are either already going to be aligned or they're going to be smaller than expected.
And if we just use the largest possible var note, we risk potential misalignment there, right?
So that's why we have the separation.
So the larger saline edge will var no will be equal to the largest primitive variable node.
So yeah, we then align the variable size to that to ensure it's aligned.
And finally, if the padding is not zero, then obviously this was padded and therefore we go bodycon
padded equals padded.
Okay, so that's our pause, the finalized body function in action.
So yeah, I hope that explains finalizing the bodies.
Please don't worry if you're still confused.
I understand that there might have been a lot.
However, if you have had trouble, just send me a message and I'll do my best to help you.
However, please don't worry if you're really confused at this point because things like this take time.
Writing a C compiler is a big job.
As we go through the course, everything will start to fall more in place and you will understand exactly
why we have to align things and why we need to pad things.
But for now, just know that we are padding and aligning for a reason.
The CE standard requires alignment of structures and unions.
Thank you for watching.