[lld][ELF] Add LOG2CEIL builtin ldscript function

This patch adds support for the LOG2CEIL builtin function in linker scripts: https://sourceware.org/binutils/docs/ld/Builtin-Functions.html#index-LOG2CEIL_0028exp_0029

As documented for LD, and to keep compatibility, LOG2CEIL(0) returns 0 (not -inf).

The test vectors are somewhat arbitrary. We check minimum values (0-4); middle values (2^32, and 2^32+1); and the maximum value (2^64-1).

The checks for LOG2CEIL explicitly use full 64-bit values (16 hex digits). This is needed to properly verify that -inf and other interesting results aren't returned. (For some reason, all other tests in operators.test use only 14 digits.)

Differential revision: https://reviews.llvm.org/D84054
This commit is contained in:
Isaac Richter
2020-07-27 11:49:24 +03:00
committed by Georgii Rymar
parent 19e472fd84
commit fa1145a8d2
2 changed files with 26 additions and 0 deletions

View File

@@ -29,6 +29,7 @@
#include "llvm/Support/Casting.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/ScopedPrinter.h"
#include <cassert>
@@ -1329,6 +1330,15 @@ Expr ScriptParser::readPrimary() {
return cmd->getLMA();
};
}
if (tok == "LOG2CEIL") {
expect("(");
Expr a = readExpr();
expect(")");
return [=] {
// LOG2CEIL(0) is defined to be 0.
return llvm::Log2_64_Ceil(std::max(a().getValue(), UINT64_C(1)));
};
}
if (tok == "MAX" || tok == "MIN") {
expect("(");
Expr a = readExpr();