got 3 arguments but expected 2 lui a1, %hi(.LJTI0_0) - hex

I am converting the dhrystone benchmark to test its performance on a riscv compiled version. I have generated the assembly code and now intend to convert it into a hexcode format using the venus-simulator.
But when i run the code on the simulator i get multiple warnings regarding the same few lines. it seems that i am passing extra arguments in the 'lui', 'addi' and a few other instructions. I cant seem to make any sense of it.
Your help is greatly appreciated.
Here is the code
Proc_6: # #Proc_6
addi sp, sp, -32
sw ra, 28(sp) # 4-byte Folded Spill
sw s0, 24(sp) # 4-byte Folded Spill
addi s0, sp, 32
sw a0, -16(s0)
sw a1, -20(s0)
lw a0, -16(s0)
lw a1, -20(s0)
sw a0, 0(a1)
lw a0, -16(s0)
call Func_3
li a1, 0
bne a0, a1, .LBB0_2
j .LBB0_1
.LBB0_1:
lw a1, -20(s0)
li a0, 3
sw a0, 0(a1)
j .LBB0_2
.LBB0_2:
lw a1, -16(s0)
sw a1, -24(s0) # 4-byte Folded Spill
li a0, 4
bltu a0, a1, .LBB0_12
lw a0, -24(s0) # 4-byte Folded Reload
slli a0, a0, 2
lui a1, %hi(.LJTI0_0)
addi a1, a1, %lo(.LJTI0_0)
add a0, a0, a1
lw a0, 0(a0)
jr a0
.LBB0_4:
lw a1, -20(s0)
li a0, 0
sw a0, 0(a1)
j .LBB0_12
.LBB0_5:
lui a0, %hi(Int_Glob)
lw a0, %lo(Int_Glob)(a0)
li a1, 101
blt a0, a1, .LBB0_7
j .LBB0_6
.LBB0_6:
lw a1, -20(s0)
li a0, 0
sw a0, 0(a1)
j .LBB0_8
.LBB0_7:
lw a1, -20(s0)
li a0, 3
sw a0, 0(a1)
j .LBB0_8
.LBB0_8:
j .LBB0_12
.LBB0_9:
lw a1, -20(s0)
li a0, 1
sw a0, 0(a1)
j .LBB0_12
.LBB0_10:
j .LBB0_12
.LBB0_11:
lw a1, -20(s0)
li a0, 2
sw a0, 0(a1)
j .LBB0_12
.LBB0_12:
lw a0, -12(s0)
lw ra, 28(sp) # 4-byte Folded Reload
lw s0, 24(sp) # 4-byte Folded Reload
addi sp, sp, 32
ret
.LJTI0_0:
.word .LBB0_4
.word .LBB0_5
.word .LBB0_9
.word .LBB0_10
.word .LBB0_11
Proc_7: # #Proc_7
addi sp, sp, -32
sw ra, 28(sp) # 4-byte Folded Spill
sw s0, 24(sp) # 4-byte Folded Spill
addi s0, sp, 32
sw a0, -16(s0)
sw a1, -20(s0)
sw a2, -24(s0)
lw a0, -16(s0)
addi a0, a0, 2
sw a0, -28(s0)
lw a0, -20(s0)
lw a1, -28(s0)
add a0, a0, a1
lw a1, -24(s0)
sw a0, 0(a1)
lw a0, -12(s0)
lw ra, 28(sp) # 4-byte Folded Reload
lw s0, 24(sp) # 4-byte Folded Reload
addi sp, sp, 32
ret
Proc_8: # #Proc_8
addi sp, sp, -48
sw ra, 44(sp) # 4-byte Folded Spill
sw s0, 40(sp) # 4-byte Folded Spill
addi s0, sp, 48
sw a0, -16(s0)
sw a1, -20(s0)
sw a2, -24(s0)
sw a3, -28(s0)
lw a0, -24(s0)
addi a0, a0, 5
sw a0, -36(s0)
lw a0, -28(s0)
lw a1, -16(s0)
lw a2, -36(s0)
slli a2, a2, 2
add a1, a1, a2
sw a0, 0(a1)
lw a0, -16(s0)
lw a1, -36(s0)
slli a1, a1, 2
add a1, a1, a0
lw a0, 0(a1)
sw a0, 4(a1)
lw a0, -36(s0)
lw a2, -16(s0)
slli a1, a0, 2
add a1, a1, a2
sw a0, 120(a1)
lw a0, -36(s0)
sw a0, -32(s0)
j .LBB2_1
.LBB2_1: # =>This Inner Loop Header: Depth=1
lw a1, -32(s0)
lw a0, -36(s0)
addi a0, a0, 1
blt a0, a1, .LBB2_4
j .LBB2_2
.LBB2_2: # in Loop: Header=BB2_1 Depth=1
lw a0, -36(s0)
lw a1, -20(s0)
li a2, 200
mul a2, a0, a2
add a1, a1, a2
lw a2, -32(s0)
slli a2, a2, 2
add a1, a1, a2
sw a0, 0(a1)
j .LBB2_3
.LBB2_3: # in Loop: Header=BB2_1 Depth=1
lw a0, -32(s0)
addi a0, a0, 1
sw a0, -32(s0)
j .LBB2_1
.LBB2_4:
lw a1, -20(s0)
lw a0, -36(s0)
li a4, 200
mul a2, a0, a4
add a1, a1, a2
slli a0, a0, 2
add a1, a1, a0
lw a0, -4(a1)
addi a0, a0, 1
sw a0, -4(a1)
lw a0, -16(s0)
lw a3, -36(s0)
slli a2, a3, 2
add a0, a0, a2
lw a0, 0(a0)
lw a1, -20(s0)
addi a3, a3, 20
mul a3, a3, a4
add a1, a1, a3
add a1, a1, a2
sw a0, 0(a1)
lui a1, %hi(Int_Glob)
li a0, 5
sw a0, %lo(Int_Glob)(a1)
lw a0, -12(s0)
lw ra, 44(sp) # 4-byte Folded Reload
lw s0, 40(sp) # 4-byte Folded Reload
addi sp, sp, 48
ret
Func_1: # #Func_1
addi sp, sp, -16
sw ra, 12(sp) # 4-byte Folded Spill
sw s0, 8(sp) # 4-byte Folded Spill
addi s0, sp, 16
sb a0, -13(s0)
sb a1, -14(s0)
lb a0, -13(s0)
sb a0, -15(s0)
lb a0, -15(s0)
sb a0, -16(s0)
lbu a0, -16(s0)
lbu a1, -14(s0)
beq a0, a1, .LBB3_2
j .LBB3_1
.LBB3_1:
li a0, 0
sw a0, -12(s0)
j .LBB3_3
.LBB3_2:
lb a0, -15(s0)
lui a1, %hi(Ch_1_Glob)
sb a0, %lo(Ch_1_Glob)(a1)
li a0, 1
sw a0, -12(s0)
j .LBB3_3
.LBB3_3:
lw a0, -12(s0)
lw ra, 12(sp) # 4-byte Folded Reload
lw s0, 8(sp) # 4-byte Folded Reload
addi sp, sp, 16
ret
Func_2: # #Func_2
addi sp, sp, -32
sw ra, 28(sp) # 4-byte Folded Spill
sw s0, 24(sp) # 4-byte Folded Spill
addi s0, sp, 32
sw a0, -16(s0)
sw a1, -20(s0)
li a0, 2
sw a0, -24(s0)
j .LBB4_1
.LBB4_1: # =>This Inner Loop Header: Depth=1
lw a1, -24(s0)
li a0, 2
blt a0, a1, .LBB4_5
j .LBB4_2
.LBB4_2: # in Loop: Header=BB4_1 Depth=1
lw a0, -16(s0)
lw a1, -24(s0)
add a0, a0, a1
lbu a0, 0(a0)
lw a2, -20(s0)
add a1, a1, a2
lbu a1, 1(a1)
call Func_1
li a1, 0
bne a0, a1, .LBB4_4
j .LBB4_3
.LBB4_3: # in Loop: Header=BB4_1 Depth=1
li a0, 65
sb a0, -25(s0)
lw a0, -24(s0)
addi a0, a0, 1
sw a0, -24(s0)
j .LBB4_4
.LBB4_4: # in Loop: Header=BB4_1 Depth=1
j .LBB4_1
.LBB4_5:
lbu a0, -25(s0)
li a1, 87
blt a0, a1, .LBB4_8
j .LBB4_6
.LBB4_6:
lbu a1, -25(s0)
li a0, 89
blt a0, a1, .LBB4_8
j .LBB4_7
.LBB4_7:
li a0, 7
sw a0, -24(s0)
j .LBB4_8
.LBB4_8:
lbu a0, -25(s0)
li a1, 82
bne a0, a1, .LBB4_10
j .LBB4_9
.LBB4_9:
li a0, 1
sw a0, -12(s0)
j .LBB4_13
.LBB4_10:
lw a0, -16(s0)
lw a1, -20(s0)
call strcmp
mv a1, a0
li a0, 0
bge a0, a1, .LBB4_12
j .LBB4_11
.LBB4_11:
lw a0, -24(s0)
addi a0, a0, 7
sw a0, -24(s0)
lw a0, -24(s0)
lui a1, %hi(Int_Glob)
sw a0, %lo(Int_Glob)(a1)
li a0, 1
sw a0, -12(s0)
j .LBB4_13
.LBB4_12:
li a0, 0
sw a0, -12(s0)
j .LBB4_13
.LBB4_13:
lw a0, -12(s0)
lw ra, 28(sp) # 4-byte Folded Reload
lw s0, 24(sp) # 4-byte Folded Reload
addi sp, sp, 32
ret
Func_3: # #Func_3
addi sp, sp, -32
sw ra, 28(sp) # 4-byte Folded Spill
sw s0, 24(sp) # 4-byte Folded Spill
addi s0, sp, 32
sw a0, -16(s0)
lw a0, -16(s0)
sw a0, -20(s0)
lw a0, -20(s0)
li a1, 2
bne a0, a1, .LBB5_2
j .LBB5_1
.LBB5_1:
li a0, 1
sw a0, -12(s0)
j .LBB5_3
.LBB5_2:
li a0, 0
sw a0, -12(s0)
j .LBB5_3
.LBB5_3:
lw a0, -12(s0)
lw ra, 28(sp) # 4-byte Folded Reload
lw s0, 24(sp) # 4-byte Folded Reload
addi sp, sp, 32
ret
debug_printf: # #debug_printf
addi sp, sp, -48
sw ra, 12(sp) # 4-byte Folded Spill
sw s0, 8(sp) # 4-byte Folded Spill
addi s0, sp, 16
sw a7, 28(s0)
sw a6, 24(s0)
sw a5, 20(s0)
sw a4, 16(s0)
sw a3, 12(s0)
sw a2, 8(s0)
sw a1, 4(s0)
sw a0, -12(s0)
lw ra, 12(sp) # 4-byte Folded Reload
lw s0, 8(sp) # 4-byte Folded Reload
addi sp, sp, 48
ret

Related

Print sum of vectors MIPS

I am trying to do exercise of MIPS, i have to sum to vector into another vector and then print the sum, I know for sure something is wrong with the sum, because the output is 0 0 0 0 0. Can someone explain me where i am wrong? Be patient please :) Thanks
.data
str: .asciiz " "
v1 : .word 3, 10, 5, 22, 13
v2 : .word 7, 1, 9, 16, 8
.text
la $a1, v1
la $a2, v2
addi $sp, $sp, -20
sw $s1, 0($sp)
add $t0, $0, $0 # i = 0
somma: slti $t1, $t0, 5 #if i < 5 continua else esci
beq $t1, $0, esci
sll $t2, $t0, 2 # i = i * 4
add $t3, $a1, $t2 # $t2 = v1 + i*4
add $t4, $a2, $t2 # $t3 = v2 + i*4
lw $t3, 0($t3) #$t3 = v1[i]
lw $t4, 0($t4) #$t4 = v2[i]
add $s1, $t3, $t4 # v3 = v1 + v2
addi $t0, $t0, 1
j somma
esci:
add $t0, $0, $0 # i = 0
stampa: slti $t1, $t0, 5 # se i<5 continua else fine
beq $t1, $0, fine
lw $a0, 0($sp)
li $v0, 1
syscall
li $v0, 4
la $a0, str
addi $t0, $t0, 1
addi $sp, $sp, 4
syscall
j stampa
fine: li $v0, 10
syscall
If your intent is to use some stack space for a temporary vector holding the sum:
addiu $sp, $sp, -20
allocates the stack space. The allocated stack space is addressable from $sp+0 through $sp+16. (Storing $s1 there is pointless, so just remove that.)
After the vector element addition into $t1, store the element sum result into the temporary vector in the stack, using indexing like you do with the source vectors:
addu $t5, $sp, $t2 # compute address of temp vector element to store
sw $t1, 0($t5)

Recursively building a list of dataframes (in R) - is loop only option?

I have a dataframe with n columns and I need to obtain combinations of its variables:
E.g.:
df <- data.frame(A = c("a1","a2","a3","a4","a5","a6"),
B = c("a1","a1","a3","a3","a5","a5"),
C = c("a1","a1","a1","a3","a4","a4"),
D = c("a1","a1","a1","a3","a4","a5"))
I need to make a list that would have n-1 elements each including all the unique combinations of the dataframe variables. The first element includes unique values for each columns starting from the first and ending to the last. For each subsequent element I need to drop the first column of the previous appended dataframe. Like this:
myList <- list(unique(df[,1:ncol(df)),
unique(df[,2:ncol(df)),
unique(df[,3:ncol(df)))
I managed to solve this with a for loop:
myList <- list()
for (i in 1:(ncol(df) - 1)){
myList[[i]] <- unique(df[, i:ncol(df)])
}
but I was left wondering whether there was a faster and more elegant way to do this.
With sapply():
sapply(1:(ncol(df)-1),
FUN = function(x, nc, df) unique(df[, x:nc]), nc = ncol(df), df = df)
An elegant solution would be a recursion:
func = function(df, n, lst)
{
if(ncol(df)==n) return(lst)
func(df, n+1, c(lst, list(unique(df[n:ncol(df)]))))
}
#> func(df,1, list())
#[[1]]
# A B C D
#1 a1 a1 a1 a1
#2 a2 a1 a1 a1
#3 a3 a3 a1 a1
#4 a4 a3 a3 a3
#5 a5 a5 a4 a4
#6 a6 a5 a4 a5
#[[2]]
# B C D
#1 a1 a1 a1
#3 a3 a1 a1
#4 a3 a3 a3
#5 a5 a4 a4
#6 a5 a4 a5
#[[3]]
# C D
#1 a1 a1
#4 a3 a3
#5 a4 a4
#6 a4 a5

add connections in graph

I have a graph stored as a dataframe that each row represents a link. Some nodes in the graph have a group of alias. What I want to do is to merge the alias information into the graph. For example a graph is
node1 node2
1 A E
2 B F
3 C G
Node A has alias A1, A2, node E has alias E1, E2, so all the connections between {A, A1, A2} and {E, E1, E2} are added to the dataframe. I think I can use the package igraph to achieve that.
Let's store the alias count in a lookup-table with row names:
> aliases=data.frame(count=c(1,2,2,3,2,1))
> row.names(aliases)=c("A","B","C","E","F","G")
This function expands a row of the data frame, and numbers the original thing with a zero:
expand.row=function(r){
e1=r[1];e2=r[2]
n1=aliases[e1,1];n2=aliases[e2,1]
expand.grid(
paste(e1,seq_len(n1)-1,sep=""),
paste(e2,seq_len(n2)-1,sep=""))
}
So that:
> expand.row(c("B","E"))
Var1 Var2
1 B0 E0
2 B1 E0
3 B0 E1
4 B1 E1
5 B0 E2
6 B1 E2
Then apply over the edge data frame, merge the bits into a single matrix:
> full = do.call(rbind,apply(as.matrix(d),1,expand.row))
> full
Var1 Var2
1 A0 E0
2 A0 E1
3 A0 E2
4 B0 F0
5 B1 F0
6 B0 F1
7 B1 F1
8 C0 G0
9 C1 G0
and there's your graph:
> g = graph.edgelist(as.matrix(full))
> plot(g)

Creating combinations of two vectors

Suppose the following situation. There are two tables, each one of them with data of different quality. Both of them have the same variables A, B and C. Variables in the first table are called A1, B1 and C2, while those in the second table are called A2, B2, and C2.
The first table can be updated with the second table. There are six possible combinations:
A1, B1, C2
A1, B2, C1
A2, B1, C1
A1, B2, C2
A2, B1, C2
A2, B2, C1
The question is how to get that in R. What I'm using is what follows:
require(utils)
require(stringr)
vars <- c("A1", "B1", "C1", "A2", "B2", "C2")
combine <- function(data, n){
com1 = combn(data, n)# make all combinations
com2 = c(str_sub(com1, end=-2L))# remove the number in the end of the name
com3 = matrix(com2, nrow = dim(com1)[1], ncol = dim(com1)[2])# vector to matrix
com3 = split(com3, rep(1:ncol(com3), each = nrow(com3)))# matrix to list
com3 = lapply(com3, duplicated)# find list elements with duplicated names
com3 = lapply(com3, function(X){X[which(!any(X == TRUE))]})# identify duplicated names
pos = which(as.numeric(com3) == 0)# get position of duplicates
com3 = com1[,pos]# return elements from the original list
com3 = split(com3, rep(1:ncol(com3), each = nrow(com3)))# matrix to list
com3 = lapply(com3, sort)# sort by alphabetical order
com3 = as.data.frame(com3, stringsAsFactors = FALSE)# matrix to data frame
res = list(positions = pos, combinations = com3)# return position and combinations
return(res)
}
combine(vars, 3)
$positions
[1] 1 4 6 10 11 15 17 20
$combinations
X1 X2 X3 X4 X5 X6 X7 X8
1 A1 A1 A1 A1 A2 A2 A2 A2
2 B1 B1 B2 B2 B1 B1 B2 B2
3 C1 C2 C1 C2 C1 C2 C1 C2
I'd like to know if anyone knows a more straightforward solution than creating all possible combinations and afterwards cleaning up the result as my function does.
You're over thinking the problem. Just use expand.grid:
> expand.grid(c('A1','A2'),c('B1','B2'),c('C1','C2'))
Var1 Var2 Var3
1 A1 B1 C1
2 A2 B1 C1
3 A1 B2 C1
4 A2 B2 C1
5 A1 B1 C2
6 A2 B1 C2
7 A1 B2 C2
8 A2 B2 C2

link records in r with least number of steps

I have a dataset with 2 columns like below.
INBOUND OUTBOUND
a1 a2
a1 a3
a1 a6
a2 a50
a4 a63
a8 a9
a10 a11
a50 a51
I wanted to give a similar id to those which are interlinked
Ex: a1 is linked to a2 , a2 is linked to a50, a50 to a51... So I would want to create a variable "id" and give same id to all these records.
I am right now doing a merge operation and marking these records. But since this is not an exhaustive search, I am able to mark only a1 --> a2, and a2 --> a50 with the same id.
So I am assuming there should be a better way to do this in one shot.
Help needed on this. Thanks
#
> dat <- read.csv("C:\\Users\\abc\\Documents\\Book1.csv", stringsAsFactors=FALSE, header = TRUE, sep = ",")
> dat
INBOUND OUTBOUND
1 a1 a2
2 a1 a3
3 a1 a6
4 a2 a50
5 a4 a63
6 a8 a9
7 a10 a11
8 a50 a51
> g <- graph.data.frame(dat)
> clusters(g)
$membership
[1]
1 2 3 4 5 6 1 1 1 2 3 4 5 6
$csize
[1]
4 2 2 2 2 2
$no
[1]
6
The clusters(g)$membership i got is this
1 2 3 4 5 6 1 1 1 2 3 4 5 6
And the one you got for the same data is
1 1 2 3 4 1 1 1 2 3 4 1
Edit: I added a much simpler answer using igraph below
Coincidentally, I happened to have a similar problem this week. I ended up using an algorithm from Numerical Recipes (section 8.6 on page 345) (the code in this edition contains some errors, by the way). However, the algorithm itself it is written in c++, so I hope you have the tools installed to compile this.
The code is given below. Using the function equivalence on you dataset:
> relations <- "INBOUND OUTBOUND
+ a1 a2
+ a1 a3
+ a1 a6
+ a2 a50
+ a4 a63
+ a8 a9
+ a10 a11
+ a50 a51"
> relations <- read.table(textConnection(relations), stringsAsFactors=FALSE, header=TRUE)
>
> source("equivalence.R")
> objects <- unique(c(relations[[1]], relations[[2]]))
> groups <- equivalence(objects, relations)
>
> data.frame(objects, groups)
objects groups
1 a1 12
2 a2 12
3 a4 9
4 a8 10
5 a10 11
6 a50 12
7 a3 12
8 a6 12
9 a63 9
10 a9 10
11 a11 11
12 a51 12
equivalence.cpp
The c++ file with the algorithm
#include <R.h>
#include <Rinternals.h>
#include <string>
extern "C" {
SEXP equivalence(SEXP ra, SEXP rb, SEXP rn) {
try {
if (LENGTH(ra) != LENGTH(rb))
throw std::string("Lengths of a and be do not match.");
int* a = INTEGER(ra);
int* b = INTEGER(rb);
int m = LENGTH(ra);
int n = INTEGER(rn)[0];
SEXP classes = PROTECT(allocVector(INTSXP, n));
int* cls = INTEGER(classes);
//Initialize each element its own class.
for (int k = 0; k < n; k++) cls[k] = k;
//For each piece of input information...
for (int l = 0; l < m; l++) {
//Track first element up to its ancestor.
int j = a[l];
while (cls[j] != j) j = cls[j];
//Track second element up to its ancestor.
int k = b[l];
while (cls[k] != k) k = cls[k];
//If they are not already related, make them so.
if (j != k) {
cls[j] = k;
}
}
//Final sweep up to highest ancestors.
for (int j = 0; j < n; j++) {
while (cls[j] != cls[cls[j]]) cls[j] = cls[cls[j]];
}
UNPROTECT(1);
return classes;
} catch(const std::string& e) {
error(e.c_str());
return R_NilValue;
} catch (...) {
error("Uncaught exception.");
return R_NilValue;
}
}
equivalence.R
The code that loads the shared library (change extension from .so to .dll if you are working under windows)
dyn.load("equivalence.so")
equivalence <- function(x, rules) {
tmp <- unique(x)
tmp <- tmp[!is.na(tmp)]
a <- match(rules[[1]], tmp)
b <- match(rules[[2]], tmp)
sel <- !is.na(a) & !is.na(b)
if (any(!sel)) {
warning("Not all values in rules are present in x.")
a <- a[sel]
b <- b[sel]
}
res <- .Call("equivalence", as.integer(a)-1L, as.integer(b)-1L,
as.integer(length(tmp)));
res[match(x, tmp)] + 1L
}
Using igraph
You can also use igraph, which is much simpler (should have thought of that before). The groups can be obtained using the clusters function and the corresponding nodes/vertices can be obtained using the V function:
> library(igraph)
> g <- graph.data.frame(relations)
> cl <- clusters(g)
> data.frame(object = V(g)$name, groups = cl$membership)
object groups
1 a1 1
2 a2 1
3 a4 2
4 a8 3
5 a10 4
6 a50 1
7 a3 1
8 a6 1
9 a63 2
10 a9 3
11 a11 4
12 a51 1

Resources