1b1994897Sopenharmony_ci# Usage of Class Hierarchy during AOT compilation
2b1994897Sopenharmony_ci
3b1994897Sopenharmony_ciAOT compiler mode mainly described in [aot.md](../../docs/aot.md), please read it first.
4b1994897Sopenharmony_ci
5b1994897Sopenharmony_ci## Class Hierarchy Matching
6b1994897Sopenharmony_ci
7b1994897Sopenharmony_ciThe class order in class path influences on resulting class hierarchy that would be available for an application. As a
8b1994897Sopenharmony_ciresult the order of files in class path and files themselves are enough to verify the class context. The order resolves
9b1994897Sopenharmony_cisituations when several same classes were passed and only the first is considered by VM. To identify ark files the adler
10b1994897Sopenharmony_cichecksum is used.
11b1994897Sopenharmony_ci
12b1994897Sopenharmony_ciThis class context is represented by a string which is written into AOT file and compared with runtime's one during AOT file loading.
13b1994897Sopenharmony_ci
14b1994897Sopenharmony_ciThe typical string is:
15b1994897Sopenharmony_ci```
16b1994897Sopenharmony_ci/mnt/scratch/build/panda/pandastdlib/arkstdlib.abc*2239629066:/mnt/scratch/build/panda/Application.abc*1345224566:
17b1994897Sopenharmony_ci```
18b1994897Sopenharmony_ci
19b1994897Sopenharmony_ci### Class path
20b1994897Sopenharmony_ci
21b1994897Sopenharmony_ciAOT file compiled with class hierarchy requires the complete conformity of class path with runtime. It means that we
22b1994897Sopenharmony_cihave to pass a class path to AOT compiler identical to class path used by runtime.
23b1994897Sopenharmony_ci
24b1994897Sopenharmony_ciThe example of usage
25b1994897Sopenharmony_ci
26b1994897Sopenharmony_ci```
27b1994897Sopenharmony_ci$ bin/ark_aot --boot-panda-files=$LIBDIR/lib1.abc:$LIBDIR/lib2.abc \
28b1994897Sopenharmony_ci              --paoc-panda-files application.abc \
29b1994897Sopenharmony_ci              --paoc-output application.an \
30b1994897Sopenharmony_ci              --paoc-use-cha=true
31b1994897Sopenharmony_ci$ bin/ark --boot-panda-files=$LIBDIR/lib1.abc:$LIBDIR/lib2.abc \
32b1994897Sopenharmony_ci          --aot-files=application.an \
33b1994897Sopenharmony_ci          application.abc \
34b1994897Sopenharmony_ci          Application::Main
35b1994897Sopenharmony_ci```
36b1994897Sopenharmony_ci
37b1994897Sopenharmony_ciThe wrong usage. Some classes were omitted during AOT compilation due to the fact that they are not used.
38b1994897Sopenharmony_ci
39b1994897Sopenharmony_ci```
40b1994897Sopenharmony_ci$ bin/ark_aot --boot-panda-files=$LIBDIR/lib1.abc \
41b1994897Sopenharmony_ci              --paoc-panda-files application.abc \
42b1994897Sopenharmony_ci              --paoc-output application.an \
43b1994897Sopenharmony_ci              --paoc-use-cha=true
44b1994897Sopenharmony_ci$ bin/ark --boot-panda-files=$LIBDIR/lib1.abc:$LIBDIR/lib2.abc \
45b1994897Sopenharmony_ci          --aot-files=application.an \
46b1994897Sopenharmony_ci          application.abc \
47b1994897Sopenharmony_ci          Application::Main
48b1994897Sopenharmony_ci[TID 0073b5] E/aot: Failed to load AoT file: Cannot use 'application.an'.
49b1994897Sopenharmony_ciRuntime class context: '$LIBDIR/lib1.abc*2239629066:$LIBDIR/lib2.abc*2767220441:application.abc*2524155584'.
50b1994897Sopenharmony_ciAoT class context: '$LIBDIR/lib1.abc*2239629066:application.abc*2524155584'
51b1994897Sopenharmony_ci```
52b1994897Sopenharmony_ci
53b1994897Sopenharmony_ciThe string of class context for AOT file takes into account `--paoc-location` option.
54b1994897Sopenharmony_ci
55b1994897Sopenharmony_ci### Boot class path
56b1994897Sopenharmony_ciThe boot class path now is a part of class context string. When boot image would be implemented the checksum of the
57b1994897Sopenharmony_ciimage should be checked during AOT file verification.
58b1994897Sopenharmony_ci
59b1994897Sopenharmony_ciAs boot class path represents the core classes of VM it should not depend on any of application code. To obtain AOT file
60b1994897Sopenharmony_ciof boot class file the options `--paoc-boot-output` should be used. The class hierarchy verification for boot libraries
61b1994897Sopenharmony_ciis performed during Runtime initialization when only boot panda files are loaded. The verification of .an
62b1994897Sopenharmony_cifiles for boot panda files is needed when the options `--enable-an` is used.
63b1994897Sopenharmony_ci
64b1994897Sopenharmony_ciSince the AOT file can be compiled on one host system and executed on different target, we need an option to specify boot
65b1994897Sopenharmony_cifiles location according their paths on the target. `--paoc-boot-location` specifies the path where boot panda files are
66b1994897Sopenharmony_cilocated on the target system. It works identically to `--paoc-location` but only for boot files used during compilation.
67b1994897Sopenharmony_ci
68b1994897Sopenharmony_ciIt is important that for boot aot files the entire --boot-panda-files must match between `ark_aot` and `ark`. It means, that if you are compiling an AOT file for boot panda files, then all other entries of --boot-panda-files that will be used during execution should have been passed as well.
69b1994897Sopenharmony_ci
70b1994897Sopenharmony_ci## Class Hierarchy Usage
71b1994897Sopenharmony_ci
72b1994897Sopenharmony_ci### Virtual Calls
73b1994897Sopenharmony_ci
74b1994897Sopenharmony_ciGenerally, the default approach of virtual call consists of the following steps:
75b1994897Sopenharmony_ci
76b1994897Sopenharmony_ci1) Get class from object
77b1994897Sopenharmony_ci2) Obtain MethodPtr of the callee virtual method
78b1994897Sopenharmony_ci3) Retrieve the index of method in virtual table
79b1994897Sopenharmony_ci4) Load the real MethodPtr from class (obtained in step 1) using index from previous step
80b1994897Sopenharmony_ci5) Invoke method
81b1994897Sopenharmony_ci
82b1994897Sopenharmony_ciFor JIT compiler steps 2-3-4 is merged into one, because it can obtain the index of method in virtual table during
83b1994897Sopenharmony_cicompilation.
84b1994897Sopenharmony_ci
85b1994897Sopenharmony_ciFor AOT compilation the method and its index in virtual table is unknown because at runtime the chain of inheritance may
86b1994897Sopenharmony_cidiffer. By this reason, the compiled code has to resolve the method during execution and retrieve index from the method.
87b1994897Sopenharmony_ciThe [PLT Resolvers](./plt.md) help to avoid this resolution on each call and cache result (index inside virtual table
88b1994897Sopenharmony_cifor each method) into `.aot_table`.
89b1994897Sopenharmony_ci
90b1994897Sopenharmony_ciThe usage of class hierarchy may help AOT compiler to squash 2-3-4 into one step as JIT-compiler does. It is possible
91b1994897Sopenharmony_cibecause the MethodPtr is needed only to obtain the virtual table index. The indices inside virtual tables are still
92b1994897Sopenharmony_ciunchanged until the class hierarchy has changed. Therefore AOT compiler can generate code similar to JIT compiler if the
93b1994897Sopenharmony_ciclass hierarchy of runtime matches the class hierarchy during AOT compilation.
94