Project Stage 2
My task is:
Command-line Parsing
Description:
Parse the GCC command line to pick up AFMV options, process the version
list to validate the architectural feature specification. (example: -fafmv=var1,var2)
One of the places to see how others did it is on GitHub of other class that did similar project task. I could also go on GCC's git to see pass commit that is could be related to my task of adding a pass.
Most of my work will be done in the .opt
file writing.
We will be using slack as a communication tool.
Link:
other class: https://github.com/seneca-cdot/gcc
GCC: https://gcc.gnu.org/onlinedocs/gccint/
GitHub: https://github.com/seneca-cdot/gcc (I work in the branch "2024-S-command-line-parsing")
GitHub branch: https://github.com/Seneca-CDOT/gcc/tree/2024-S-command-line-parsing
I will be using the class server. I don think I want to spend time on trying to do it on my local device. This term of SPO600 they cut the time in half. We are very time limited.
This stage we will be mostly working in our own branch, we will be putting them together on stage 3.
I am trying to look for the winter term SPO600's branch but find seem to find one for Command line Parsing.
First fork the class gcc github where your branch is, than pull it in to the class server.
I did "ls *common*" to see all the file that has "common" is it.
Go in to ~/gcc/common.opt we will be adding our AFMV options here.
Use vim to read/write the common.opt (this file is mostly in alphabetical order)
I added "fafmv=" right before the "fhelp" (433)
(trying which way works out of the two)
fafmv=
Common Joined Var(flag_fafmv) Optimization
Function automatic multiversioning
Common Joined Var(flag_fafmv) Optimization
Function automatic multiversioning
- or -
-fafmv
Common Var(flag_afmv) Init(1)
Function automatic multiversioning
Common Var(flag_afmv) Init(1)
Function automatic multiversioning
Now we have to go to the opts.cc to make the case for our fafmv option. Got to check if the command pass in any options. reutrn error if no options is pass with it.
Using vim in the opts.cc I search for OPT_ and look around. It seems like every pass has it's own swtich case and they are also in alphabetical order. I added a case for fafmv.
I looked at "OPT_Wattributes_" and how they handle errors and copy it. hopefully it will work.
In the "common_handle_option" they have arg so I will use that to see if that will get the options.
(trying which way works out of the two)
case OPT_fafmv_:
if (arg){
printf("arg: %s\n", arg);
} else {
error_at(loc, "Missing option");
}
break;
if (arg){
printf("arg: %s\n", arg);
} else {
error_at(loc, "Missing option");
}
break;
- or -
case OPT__fafmv:
if (arg){
printf("arg: %s\n", arg);
} else {
error_at(loc, "Missing option");
}
break;
if (arg){
printf("arg: %s\n", arg);
} else {
error_at(loc, "Missing option");
}
break;
For now I just want to see if this works. when no option is pass throw an error and if something is pass print it out.
Do "time make -j##" (# is number of CPU) and "make install" after.
than make a simple helloworld.c for compile.
Run "~/gcc-build-001/bin/gcc -fafmv=abc,123 -o test hello.c".
I found out that GGC will use a "-" on default. I don't have to put a "-" in the common.opt file for "fafmv=".
Option one works:
common.opt
common.opt
fafmv=
Common Joined Var(flag_fafmv) Optimization
Function automatic multiversioning
Common Joined Var(flag_fafmv) Optimization
Function automatic multiversioning
opts.cc
case OPT_fafmv_:
if (arg){
printf("arg: %s\n", arg);
} else {
error_at(loc, "Missing option");
}
break;
if (arg){
printf("arg: %s\n", arg);
} else {
error_at(loc, "Missing option");
}
break;
now we have to make a check for the input.
- make a list
- split the input with ","
- a flag to keep in track match or not
- loop thought arg (every loop check if it match)
- pass if arg in the list else fail
On aarch64 it supports simd, neon, sve, sve2.(https://en.wikipedia.org/wiki/AArch64)
case OPT_fafmv_:
if (!arg) {
error_at(loc, "Missing option");
} else {
const char* supported_list[] = {
"simd", "neon", "sve", "sve2"
};
int list_len = sizeof(supported_list) / sizeof(supported_list[0]);
char* features = xstrdup(arg);
char* feature = strtok(features, ",");
bool is_valid = true;
while (feature != NULL && is_valid) {
bool is_found = false;
for (int i = 0; i < list_len; i++) {
if (strcmp(feature, supported_list[i]) == 0){
is_found = true;
break;
}
}
if (!is_found) {
error_at(loc, "Unsupported option '%s'", feature);
is_valid = false;
}
feature = strtok(NULL, ",");
}
free(features);
if (is_valid) {
printf("All options are valid.\n");
} else {
break;
}
}
break;
if (!arg) {
error_at(loc, "Missing option");
} else {
const char* supported_list[] = {
"simd", "neon", "sve", "sve2"
};
int list_len = sizeof(supported_list) / sizeof(supported_list[0]);
char* features = xstrdup(arg);
char* feature = strtok(features, ",");
bool is_valid = true;
while (feature != NULL && is_valid) {
bool is_found = false;
for (int i = 0; i < list_len; i++) {
if (strcmp(feature, supported_list[i]) == 0){
is_found = true;
break;
}
}
if (!is_found) {
error_at(loc, "Unsupported option '%s'", feature);
is_valid = false;
}
feature = strtok(NULL, ",");
}
free(features);
if (is_valid) {
printf("All options are valid.\n");
} else {
break;
}
}
break;
"make" it and "make install"
test it out with the same command:
~/gcc-build-001/bin/gcc -fafmv=abc,123 -o test hello.c
~/gcc-build-001/bin/gcc -fafmv=sve,sve2 -o test hello.c
Conclusion
I have learned how to add a parse in gcc, parse that can take in options. Where things are located in the gcc tree. Better understanding of how it works. Building gcc can take a long time. I also have a refresh on how to use VIM which is fun. I have learn so much in this course and it was very fun. wish it was a 14 week course. There are too many information to take in with 7 weeks. chris tyler is the best teacher I had so far. I really enjoy his class.
Maybe Useful Links:
Getopt: http://www.gnu.org/software/libc/manual/html_node/Getopt.html
Argp: http://www.gnu.org/software/libc/manual/html_node/Argp.html
gcc-options-parser: https://github.com/milahu/gcc-options-parser
Comments
Post a Comment