Correct spelling.
[SquirrelJME.git] / assets / developer-notes / stephanie-gawroriski / 2017 / 05 / 13.mkd
blobeed75befb994f6f4e2e99fb50eb7e0e4ed9fb16c
1 # 2017/05/13
3 ## 15:34
5 Ok so, I still need to think of a better way to do this. One which is not
6 insanely complex to write like SSA, but not insanely unoptimized like a
7 purely naive approach. I also want it to be single pass for the most part if
8 when possible. I think at best there would be two passes.
10 ## 15:45
12 Ok, so a compiler is very complex to write. Well it can be easy but there are
13 many considerations when writing them. Actually I have an idea. I can do
14 register allocation at the start, which would be faster. But I can have
15 alternative execution points in methods. Basically, I run through the entire
16 method and I perform stack caching and register reallocation and such.
17 However for backward jumps instead of having some kind of anti-aliasing
18 barrier I can instead just generate extra machine code that does not have the
19 given aspects of aliasing. So basically for points which are targets of jumps
20 I have a copy of the state of variables and their alias state. If the
21 variables are not compatible alias wise then an alternative chunk is executed.
22 This means that I need basic blocks which are at the start of every jump
23 target. This way, I will never need to juggle aliases and such. Since jumps
24 are always to the start of a basic block I do not need to worry about inter
25 instructions. Exceptions also only need to be checked after exceptional
26 conditions also. But they still all need instancing to determine which
27 exceptions get to be handled. I think one of the potentially slowest things
28 would be instanceof checks.
30 ## 16:02
32 There is this Stack Overflow response:
34 https://stackoverflow.com/questions/26335959/26337040#26337040
36 So basically, there is an index for each class which specifies the number of
37 classes that are supertypes in the class. There is a list of superclasses for
38 each class. If the source class has a depth greater than or equal to the
39 target class then the cast may succeed. If it does then the depth of the
40 target class (which is lower) is compared with the two table positions. If
41 they refer to the same classes then the cast is OK. The only thing then are
42 interfaces.
44 ## 16:11
46 I could put this stuff in methods, but it would probably be best to check
47 things.
49 ## 16:12
51 Ok, so at this point the following must happen: The stack map table must be
52 read in a type specifying matter and checked that way. In order to make the
53 generated code much easier to work with, I need to know the actual types
54 which are used everywhere.
56 ## 16:14
58 Basically there is `Object_variable_info` which contains the type of an
59 `Object`.
61 ## 16:17
63 Basically as execution continues, I need to know of all the types. This means
64 that for the index table of imports can also have static cast and instance
65 checks. It can only go in certain directions though. Basically for any type
66 which is known I can have a static check whether a given cast is always
67 invalid. So take for example there is an object of the type `Double` which is
68 verified. The programmer wants to cast it to `Float`. Basically there would
69 be a table entry which basically has a flag indicating if casting from
70 `Double` to `Float` is valid. When the byte code performs the check it will
71 see if the throw an exception flag is set. If it is then it will throw a cast
72 class exception or say the `instanceof` fails. But I can also have a flag for
73 a completely safe cast.
75 ## 16:29
77 So if the cast is completely safe, it does not need to be checked. Otherwise
78 it checks if it is not never safe, in which case it performs the expensive
79 check. Then throws the exception or fails the instance.
81 ## 16:32
83 So the code is basically:
85         if (A.isNotAnAlwaysSafeCastTo(B))
86         {
87                 if (A.isAlwaysAFailingCastTo(B) || A.cannotBeCastedTo(B))
88                         throw new ClassCastException(A + " -> " + B);
89         }
91 ## 16:45
93 Also, I will need a check and flag for whether a given class is an interface
94 or not. If a class is an interface then a more complex check is required for
95 casting I believe.
97 ## 16:53
99 So this actually makes things far easier. I just then need to layer this on
100 top of the anti-aliasing code.
102 ## 17:00
104 So essentially what I have are basic block queues with entry points. All
105 blocks are pushed to a queue then compiled. Compilation is complete when all
106 basic blocks have been compiled. I would potentially need natural control flow
107 markers however to prevent potential falling into another block of code where
108 it is not intended to flow into. So jump targets are important here along with
109 the type information!
111 ## 17:53
113 So I suppose what I need is a slight alternative to the cache and register
114 states depending on the basic blocks. Since this is a new way to do things the
115 old CacheStates system will not work at all. Something that manages the basic
116 block queue and potential positions. Basically what I have is a chunk of byte
117 code and I need a basic block queue with the aliasing information.
119 ## 18:82
121 Actually to, a new `JavaType` could combine the normal types, objects, and
122 have native type imformation (pointer stuff). That way there is just a single
123 class instead of two. The class is going to become more complex due to the
124 fact that classes will be specified in it.