Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Late Externalization for STRONG externalization type #73

Merged
merged 1 commit into from
Jul 11, 2024

Conversation

giulianobelinassi
Copy link
Collaborator

Take the following code as example:

\#include <stdio.h>
int main(void) {
  printf("Hello, world\n");
  return 0;
}

And assume we want to strongly externalize the function printf. The resulting code would be:

\#define _STDIO_H	1
\#define __GLIBC_INTERNAL_STARTING_HEADER_IMPLEMENTATION
\#include <bits/libc-header-start.h>
\#define __need_size_t
\#define __need_NULL
\#include <stddef.h>
\#define __need___va_list
\#include <stdarg.h>
\#include <bits/types.h>
\#include <bits/types/__fpos_t.h>
\#include <bits/types/__fpos64_t.h>
\#include <bits/types/__FILE.h>
\#include <bits/types/FILE.h>
\#include <bits/types/struct_FILE.h>
\# include <bits/types/cookie_io_functions_t.h>
\#define L_tmpnam 20
\#include <bits/stdio_lim.h>
\#define __attr_dealloc_fclose __attr_dealloc (fclose, 1)
/** clang-extract: from /usr/include/stdio.h:361:1  */
static int (*klpe_printf)(const char *restrict, ...);

\#include <bits/floatn.h>
/** clang-extract: from /tmp/rewrite-3.c:5:1  */
int main(void)
{
  (*klpe_printf)("Hello, world!\n");
  return 0;
}

Clearly, those headers and defines could be avoided. For example, this would be perfectly valid:

\#include <stdio.h>
/** clang-extract: from /tmp/rewrite-3.c:5:1  */
static int (*klpe_printf)(const char *restrict, ...);

/** clang-extract: from /tmp/rewrite-3.c:4:1  */
int main(void)
{
  (*klpe_printf)("Hello, world!\n");
  return 0;
}

Because the definition of klpe_printf is located right before its first use. This commit does this by computing a possible place where the externalized variable can be output just before the first use, hence we externalize the variable later than the original code would suggest.

To enable this option, the user must pass -DCE_LATE_EXTERNALIZE to clang-extract.

}
}

void SymbolExternalizer::Compute_SymbolsMap_Late_Insert_Locations(std::vector<SymbolUpdateStatus *> &array)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe array as argument name could be improved? I mean, when looking at the code, the name array doesn't say much about what is containers.

Take the following code as example:
```
\#include <stdio.h>
int main(void) {
  printf("Hello, world\n");
  return 0;
}
```
And assume we want to strongly externalize the function `printf`.
The resulting code would be:
```
\#define _STDIO_H	1
\#define __GLIBC_INTERNAL_STARTING_HEADER_IMPLEMENTATION
\#include <bits/libc-header-start.h>
\#define __need_size_t
\#define __need_NULL
\#include <stddef.h>
\#define __need___va_list
\#include <stdarg.h>
\#include <bits/types.h>
\#include <bits/types/__fpos_t.h>
\#include <bits/types/__fpos64_t.h>
\#include <bits/types/__FILE.h>
\#include <bits/types/FILE.h>
\#include <bits/types/struct_FILE.h>
\# include <bits/types/cookie_io_functions_t.h>
\#define L_tmpnam 20
\#include <bits/stdio_lim.h>
\#define __attr_dealloc_fclose __attr_dealloc (fclose, 1)
/** clang-extract: from /usr/include/stdio.h:361:1  */
static int (*klpe_printf)(const char *restrict, ...);

\#include <bits/floatn.h>
/** clang-extract: from /tmp/rewrite-3.c:5:1  */
int main(void)
{
  (*klpe_printf)("Hello, world!\n");
  return 0;
}
```
Clearly, those headers and defines could be avoided. For example, this would be perfectly valid:
```
\#include <stdio.h>
/** clang-extract: from /tmp/rewrite-3.c:5:1  */
static int (*klpe_printf)(const char *restrict, ...);

/** clang-extract: from /tmp/rewrite-3.c:4:1  */
int main(void)
{
  (*klpe_printf)("Hello, world!\n");
  return 0;
}
```
Because the definition of `klpe_printf` is located right before its
first use. This commit does this by computing a possible place where
the externalized variable can be output *just before* the first use,
hence we externalize the variable later than the original code would
suggest.

To enable this option, the user must pass `-DCE_LATE_EXTERNALIZE` to
clang-extract.

Signed-off-by: Giuliano Belinassi <[email protected]>
@giulianobelinassi giulianobelinassi merged commit 74a63c6 into main Jul 11, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants